vendredi 11 septembre 2015

Total time in python's line profiler is strange

This is my insertion.py:

import random

#@profile
def insertion_sort(l):
    for j in range(1, len(l)):
        k = l[j]
        i = j - 1
        while i >= 0 and l[i] > k:
            l[i + 1] = l[i]
            i -= 1
        l[i + 1] = k


if __name__ == '__main__':
    l = range(5000)
    random.shuffle(l)
    insertion_sort(l)

When I run time python insertion.py, I get:

real    0m0.823s
user    0m0.818s
sys     0m0.004s

But when I uncomment the profile decorator and run: kernprof -l -v insertion.py, I get:

Wrote profile results to insertion.py.lprof
Timer unit: 1e-06 s

Total time: 7.25971 s
File: insertion.py
Function: insertion_sort at line 4

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
 4                                           @profile
 5                                           def insertion_sort(l):
 6      5000         2110      0.4      0.0      for j in range(1, len(l)):
 7      4999         1929      0.4      0.0          k = l[j]
 8      4999         1719      0.3      0.0          i = j - 1
 9   6211255      2695158      0.4     37.1          while i >= 0 and l[i] > k:
10   6206256      2396675      0.4     33.0              l[i + 1] = l[i] 
11   6206256      2160158      0.3     29.8              i -= 1
12      4999         1959      0.4      0.0          l[i + 1] = k

My question is why total time of line profiler is much greater than the system time? I thought "Total time" of line profiler was describing how much time the function decorated with @profile was running. In my head, the output from time should be greater or at least close to line profiler. Am I interpreting the results wrong? Is line profiler adding its own time to "Total time"?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire