Skip to content

Instantly share code, notes, and snippets.

@sdouglas
Created July 26, 2011 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdouglas/1107014 to your computer and use it in GitHub Desktop.
Save sdouglas/1107014 to your computer and use it in GitHub Desktop.
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():
''.join(L)
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