This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author dmoore
Recipients dmoore
Date 2013-11-04.18:07:30
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1383588451.25.0.545990193953.issue19495@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2013-11-04 18:07:31dmooresetrecipients: + dmoore
2013-11-04 18:07:31dmooresetmessageid: <1383588451.25.0.545990193953.issue19495@psf.upfronthosting.co.za>
2013-11-04 18:07:31dmoorelinkissue19495 messages
2013-11-04 18:07:30dmoorecreate