Created
September 28, 2013 01:03
-
-
Save ironpythonbot/6737255 to your computer and use it in GitHub Desktop.
CodePlex Issue #19484 Plain Text Attachments
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
from timeit import Timer | |
class Timing: | |
def __init__(self, name, num, init, statement): | |
self.__timer = Timer(statement, init) | |
self.__num = num | |
self.name = name | |
self.statement = statement | |
self.__result = None | |
def timeit(self): | |
self.__result = self.__timer.timeit(self.__num) | |
def getResult(self): | |
return self.__result | |
def times(num=1000000, reverse=False, init='', **statements): | |
# time each statement | |
timings = [] | |
for n, s in statements.iteritems(): | |
t = Timing(n, num, init, s) | |
t.timeit() | |
timings.append(t) | |
# print results | |
timings.sort(key=Timing.getResult, reverse=reverse) | |
for t in timings: | |
print "%s => %.3f s" % (t.name, t.getResult()) | |
times(init='l=range(1,1000)', s1='if l!=[]: pass', s2='if l: pass', s3='if len(l): pass') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment