[New-bugs-announce] [issue19495] Enhancement for timeit: measure time to run blocks of code using 'with'
Damien Moore
report at bugs.python.org
Mon Nov 4 19:07:31 CET 2013
New submission from Damien Moore:
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.
----------
components: Library (Lib)
messages: 202151
nosy: dmoore
priority: normal
severity: normal
status: open
title: Enhancement for timeit: measure time to run blocks of code using 'with'
type: enhancement
versions: Python 2.6, Python 2.7, Python 3.1, Python 3.2, Python 3.3, Python 3.4, Python 3.5
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue19495>
_______________________________________
More information about the New-bugs-announce
mailing list