Skip to content

Instantly share code, notes, and snippets.

@torstenrudolf
Last active October 23, 2020 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save torstenrudolf/277e98df296f23ff921c to your computer and use it in GitHub Desktop.
Save torstenrudolf/277e98df296f23ff921c to your computer and use it in GitHub Desktop.
import time
from collections import Counter
def fCounter(dictList):
c = Counter()
t1 = time.time()
for d in dictList:
c.update(d)
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()
},
dictList
), 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