Created
August 30, 2013 12:21
-
-
Save kissgyorgy/6389313 to your computer and use it in GitHub Desktop.
Python: timing context manager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Blog post: http://www.huyng.com/posts/python-performance-analysis/ | |
import time | |
class Timer(object): | |
def __init__(self, verbose=False): | |
self.verbose = verbose | |
def __enter__(self): | |
self.start = time.time() | |
return self | |
def __exit__(self, *args): | |
self.end = time.time() | |
self.secs = self.end - self.start | |
self.msecs = self.secs * 1000 # millisecs | |
if self.verbose: | |
print 'elapsed time: %f ms' % self.msecs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from timer import Timer | |
from redis import Redis | |
rdb = Redis() | |
with Timer() as t: | |
rdb.lpush("foo", "bar") | |
print "=> elasped lpush: %s s" % t.secs | |
with Timer as t: | |
rdb.lpop("foo") | |
print "=> elasped lpop: %s s" % t.secs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment