Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import time
from collections import Counter
def fCounter(dictList):
c = Counter()
t1 = time.time()
for d in dictList:
return c, time.time() - t1
def fReduce(dictList):
t1 = time.time()
return reduce(
lambda x, y: {
k: x[k] + y[k]
for k in dictList[0].iterkeys()
), time.time() - t1
def fSum(dictList):
t1 = time.time()
return (
{k: sum(d[k] for d in dictList) for k in dictList[0]}, time.time() - t1
def test(dictList, num=1000):
tc = 0
tr = 0
ts = 0
for i in xrange(num):
tc += fCounter(dictList)[1]
tr += fReduce(dictList)[1]
ts += fSum(dictList)[1]
return {'counter': tc / num, 'reduce': tr / num, 'sum': ts / num}
if __name__ == '__main__':
dictList = [{'a': x, 'b': 2*x, 'c': x**2} for x in xrange(10000)]
return test(dictList)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment