#!/usr/bin/env python
from operator import itemgetter
import random
from time import time

N = 10
M = 500000
KEY = 3

unsortedDict = [dict([(j,random.random()) for j in xrange(N)]) for i in xrange(M)]

d = unsortedDict[:]
start=time()
d.sort(key=itemgetter(KEY))
print '%f seconds' % (time()-start)

d = unsortedDict[:]
start=time()
d = sorted(d,key=itemgetter(KEY))
print '%f seconds' % (time()-start)

d = unsortedDict[:]
start=time()
d.sort(key=lambda k:k[KEY])
print '%f seconds' % (time()-start)

d = unsortedDict[:]
start=time()
d = sorted(d,key=lambda k:k[KEY])
print '%f seconds' % (time()-start)

d = unsortedDict[:]
start=time()
d.sort(lambda x,y : cmp(x[KEY], y[KEY]))
print '%f seconds' % (time()-start)

d = unsortedDict[:]
start=time()
d = sorted(d, lambda x,y : cmp(x[KEY], y[KEY]))
print '%f seconds' % (time()-start)

def compare_by(fieldname):
    def compare_two_dicts (a, b):
       return cmp(a[fieldname], b[fieldname])
    return compare_two_dicts

d = unsortedDict[:]
start=time()
d.sort(compare_by(KEY))
print '%f seconds' % (time()-start)

d = unsortedDict[:]
start=time()
d = sorted(d,compare_by(KEY))
print '%f seconds' % (time()-start)
