Skip to content

Instantly share code, notes, and snippets.

@arthurk
Created March 20, 2010 22:30
Show Gist options
  • Save arthurk/338955 to your computer and use it in GitHub Desktop.
Save arthurk/338955 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Benchmark: lambda vs. operator.itemgetter
from datetime import datetime
from random import randint
from timeit import Timer
def create_random_datetime_dict():
return [(i, datetime.fromtimestamp(randint(1230768000, 1262303999)))
for i in xrange(200000)]
if __name__=='__main__':
#t1 = Timer("items=create_random_datetime_dict(); items.sort(key=lambda item: item['date'])",
# "from __main__ import create_random_datetime_dict")
#print t1.timeit(10)
t2 = Timer("items=create_random_datetime_dict(); items.sort(key=itemgetter(1))",
"from __main__ import create_random_datetime_dict; from operator import itemgetter")
print t2.timeit(25)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment