Last active
January 15, 2016 20:49
-
-
Save franciscod/86c41b663cd9f88a5c4b to your computer and use it in GitHub Desktop.
simple python context manager for measuring time
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
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