Saturday, 15 September 2012

python - timeit.timer giving far different results when using it in my own function vs. calling it from the command line -



python - timeit.timer giving far different results when using it in my own function vs. calling it from the command line -

i made little function using timeit lazy , less typing isn't panning out planned.

the (relevant) code:

def timing(function, retries=10000, formatseconds=3, repeat=10): """test how long function takes run. defaults set run 10 times of 10000 tries each. display time 1 of 4 types. 0 = seconds, 1 = milliseconds, 2= microseconds , 3 = nanoseconds. pass in paramaters as: (function, retries=10000,formatseconds=3, repeat=10)""" t = timeit.timer(lambda: function) result = t.repeat(repeat=repeat,number=retries) rlist = [i/retries in result]

it runs fine keeps returning:

timeprofile.timing(find_boundaries(numpy.asarray(image.open( r'd:\python\image\image4.jpg')),79)) 10 runs of 10000 cycles each: best time: 137.94764 worst:158.16651 avg: 143.25466 nanosecs/pass

now, if interpreter:

import timeit timeit import timer t = timeit.timer(lambda: (find_boundaries(numpy.asarray(image.open(r'd:\python\image\image4.jpg')),79))) result = t.repeat(repeat=5,number=100) result = [i/100 in result]

i end [0.007723014775432375, 0.007615270149786965, 0.0075242365377505395, 0.007420834966038683, 0.0074086862470653615], or 8 milliseconds.

and if run profiler on script, gives approximately same result of 8 milliseconds.

i'm not sure problem although reckon has how it's calling function. when check info in debugger shows function dictionary len of 53, , each key contains 1 15 tuples pair of 2-3 digit numbers in each.

so, if knows why it's doing , explain me, , how prepare it, that'd great!

yes, there difference. when run:

timeprofile.timing(find_boundaries(numpy.asarray(image.open( r'd:\python\image\image4.jpg')),79))

you not passing in function reference. calling function , instead passing in result of call. timing staticresult instead of somefunction(with, arguments).

move out lambda:

timeprofile.timing(lambda: (find_boundaries(numpy.asarray(image.open( r'd:\python\image\image4.jpg')),79)))

this means need remove timing function, , instead pass function straight timer() class:

t = timeit.timer(function)

python timeit

No comments:

Post a Comment