I have a function that is called about 1742 times but when i do a decorator to calculate each call time of it i found that it prints only 647 times i don't know why that happens.
Update:
My problem here is not with how to make timing its with the difference between the number of calls, I want a way to make a decorator like function that have accurate calls.
My python version is 2.6 and here is the module i am working on http://pastebin.com/MXu1pLWM
In the profiling output i found that the caller function to the decorated function calls it only 647 and it was the only caller function!!.
Figured out!
number = 0
def timing(f):
def wrap(*args):
time1 = time.time()
ret = f(*args)
time2 = time.time()
global number
number+=1
print '%s function took %0.6f ms No of calls: %s' % (f.func_name, ((time2-time1)), str(number))
return ret
return wrap
I saw your pasted code - on the pastebin. The problem is that you are wrapping the __iter__
method of a class. It is only called once for each loop that is started. And it shoud return imediatelly with an iterator object -- what would be profilable is the calls to the "next" method on the object returned by __iter__
(this is what is called in each for
loop interacton).
Which means that without changing anything, instead of using your profiler as a decorator on a call to __iter__
you'd use that code in some other way around the yield
statement inside taht __iter__
Other than that, you will be better of using timeit.timeit or other already existing profiles to avoid such traps, as are listed on the comments.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.