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 <rep...@bugs.python.org> <http://bugs.python.org/issue19495> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com