Created
July 26, 2011 15:27
-
-
Save sdouglas/1107014 to your computer and use it in GitHub Desktop.
Performance comparisons using timeit
This file contains hidden or 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 | |
# 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