Skip to content

Instantly share code, notes, and snippets.

@alexlouden
Created June 11, 2013 09:07
Show Gist options
  • Save alexlouden/5755502 to your computer and use it in GitHub Desktop.
Save alexlouden/5755502 to your computer and use it in GitHub Desktop.
import timeit
setup = """
import random, operator
class Test():
def __init__(self, t):
self.test = t
l = [Test(str(i)) for i in range(1000)]
random.shuffle(l)
testop = operator.attrgetter('test')
"""
compare1 = "ls = sorted(l, key=lambda x: x.test)"
compare2 = "ls = sorted(l, key=testop)"
print timeit.timeit(compare1, setup, number=10000) # 6.57358211728
print timeit.timeit(compare2, setup, number=10000) # 6.8481921585
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment