Skip to content

Instantly share code, notes, and snippets.

@ironpythonbot
Created September 28, 2013 01:03
Show Gist options
  • Save ironpythonbot/6737255 to your computer and use it in GitHub Desktop.
Save ironpythonbot/6737255 to your computer and use it in GitHub Desktop.
CodePlex Issue #19484 Plain Text Attachments
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