Last active
October 23, 2020 22:00
-
-
Save torstenrudolf/277e98df296f23ff921c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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