Skip to content

Instantly share code, notes, and snippets.

@franciscod
Last active January 15, 2016 20:49
Show Gist options
  • Save franciscod/86c41b663cd9f88a5c4b to your computer and use it in GitHub Desktop.
Save franciscod/86c41b663cd9f88a5c4b to your computer and use it in GitHub Desktop.
simple python context manager for measuring time
import time
import sys
from contextlib import contextmanager
@contextmanager
def clockit(msg):
s = time.clock()
yield
print("** %s: %.03f" % (msg, time.clock() - s), file=sys.stderr)
if __name__ == '__main__':
print("hello this is a cool program")
print("small multiplication is fast")
print(" - starting small mul")
s = 1
for n in range(1,100):
s *= n
print(" - finished small mul")
print("but big multiplication is slooow")
print(" - starting big mul")
with clockit('big multiplication'):
s = 1
for n in range(1,100000):
s *= n
print(" - finished big mul")
print("see?")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment