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 ezio.melotti
Recipients dmoore, ezio.melotti, georg.brandl, giampaolo.rodola, gvanrossum, ncoghlan, rhettinger, steven.daprano, tim.peters
Date 2014-06-20.03:41:49
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1403235709.94.0.406955484168.issue19495@psf.upfronthosting.co.za>
In-reply-to
Content
I often write code like:
  import time
  start = time.time()
  ...
  end = time.time()
  print(end - start)

Usually I don't do this to measure accurately the performance of some piece of code, but rather I do it for tasks that take some time (e.g. downloading a file, or anything that I can leave there for a while and come back later to see how long it took).

So I'm +1 on a simple context manager that replaces this common snippet, and -0 on something that tries to measure accurately some piece of code (if it takes a few seconds or more, high-accuracy doesn't matter; if it takes a fraction of seconds, I won't trust the result without repeating the measurement in a loop).

Regarding the implementation I can think about 2 things I might want:
1) a way to retrieve the time (possibly as a timedelta-like object [0]), e.g.:
    with elapsed_time() as t:
        ...
    print(t.seconds)
2) a way to print a default message (this could also be the default behavior, with a silent=True to suppress the output), e.g.:
    >>> with elapsed_time(print=True):
    ...     ...
    ...
    Task terminated after X seconds.

For the location I think that the "time" module would be the first place where I would look (since I would have to otherwise import time() from there).  I would probably also look at "timeit" (since I'm going to time something), even though admittedly it doesn't fit too much with the rest.  While it might fit nicely in "contextlib", I won't probably expect to find it there unless I knew it was there in the first place.


[0] would making timedelta a context manager be too crazy?
History
Date User Action Args
2014-06-20 03:41:50ezio.melottisetrecipients: + ezio.melotti, gvanrossum, tim.peters, georg.brandl, rhettinger, ncoghlan, giampaolo.rodola, steven.daprano, dmoore
2014-06-20 03:41:49ezio.melottisetmessageid: <1403235709.94.0.406955484168.issue19495@psf.upfronthosting.co.za>
2014-06-20 03:41:49ezio.melottilinkissue19495 messages
2014-06-20 03:41:49ezio.melotticreate