Simple timeit decorator
By Maxime Biais, Tuesday 30 January 2007 at 07:54 :: Python :: #24 :: rss
A very simple decorator to notify users (no profiling) about the execution time of some parts of your code.
import time def timemeth(func): "Internal class method timing decorator" def _inner(self, *args, **kw): start = time.time() res = func(self, *args, **kw) self.timed = time.time() - start return res return _inner class TimedClass: def __init__(self): self.timed = 0 class Sample(TimedClass): @timemeth def learn(self): for i in range(1000000): i * i if __name__ == "__main__": s = Sample() print "Learning sample..." s.learn() print "Learning done ! (%.2f secs)" % s.timed
outputs:
$ python timedclass.py Learning sample... Learning done ! (2.01 secs)




Comments
1. Tuesday 30 January 2007 at 12:55, by Justin
2. Tuesday 30 January 2007 at 13:03, by maxime
3. Tuesday 30 January 2007 at 13:54, by Justin
4. Tuesday 30 January 2007 at 14:21, by maxime
Write your comment