Skip to content

Instantly share code, notes, and snippets.

@voidabhi
Last active February 19, 2016 15:40
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 voidabhi/f45f8494898f82fdf66c to your computer and use it in GitHub Desktop.
Save voidabhi/f45f8494898f82fdf66c to your computer and use it in GitHub Desktop.
Benchmarking for python functions
import time
def print_numbers(n):
for i in xrange(n):
pass
class BM(object):
def __init__(self, name):
self.name = name
def __enter__(self):
self.start = time.time()
def __exit__(self, *args):
print("{0} took {1}".format(self.name, time.clock() - self.start))
with BM('print_numbers'):
for i in xrange(10000000):
print_numbers(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment