Created
December 12, 2013 01:13
-
-
Save malanjp/7921647 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
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 | |
with Timer() as t: | |
i = 0 | |
while i < 1000000: | |
print i | |
i += 1 | |
print 'while =', t.secs | |
with Timer() as t: | |
for i in range(1000000): | |
print i | |
print 'for =', t.secs | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment