Message202151
It would be useful if timeit had a class like `timeblock` below that would allow one to easily time code inside a `with` block:
import time
class timeblock:
def __init__(self,descr=''):
self.descr=descr
def __enter__(self):
self.t0=time.time()
return self
def __exit__(self, type, value, traceback):
self.t1=time.time()
self.elapsed = self.t1-self.t0
if self.descr:
print self.descr,'took',self.elapsed,'seconds'
This would be used as follows:
with timeblock('cumsum') as t:
a=0
for x in range(10000000):
a+=x
and would output:
cumsum took 2.39 seconds
This is useful when trying to find bottlenecks in large programs without interfering with their operation, which would be harder to do with timeit.timeit and more verbose with time. |
|
Date |
User |
Action |
Args |
2013-11-04 18:07:31 | dmoore | set | recipients:
+ dmoore |
2013-11-04 18:07:31 | dmoore | set | messageid: <1383588451.25.0.545990193953.issue19495@psf.upfronthosting.co.za> |
2013-11-04 18:07:31 | dmoore | link | issue19495 messages |
2013-11-04 18:07:30 | dmoore | create | |
|