Skip to content

Instantly share code, notes, and snippets.

@samarthbhargav
Created December 18, 2014 05:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samarthbhargav/ad106974009b2c8254f3 to your computer and use it in GitHub Desktop.
Save samarthbhargav/ad106974009b2c8254f3 to your computer and use it in GitHub Desktop.
a simple profiler for python
# source: 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
# Usage:
with Timer() as t:
print "Hello World"
print t.msecs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment