Skip to content

Instantly share code, notes, and snippets.



Created Jul 26, 2011
What would you like to do?
Performance comparisons using timeit
#!/usr/bin/env python
# encoding: utf-8
import random
import string
from array import array
from timeit import Timer
repeats = 3 # report the best of these repeats
executions = 100000
L = [random.choice(string.letters) for i in range(100)]
def timeReduce():
reduce(lambda x, y: x + y, L, '')
def timeJoin():
def timeArray():
array('c', L).tostring()
if __name__ == '__main__':
func_list = [locals()[key] for key in locals().keys() \
if callable(locals()[key]) and key.startswith('time')]
for f in func_list:
t = Timer(f)
print "%s:\t%f" % (f.__name__, min(t.repeat(repeats, executions)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment