Last active
October 7, 2025 23:31
-
-
Save ggmartins/49baa3b4cfceef38857f98336106dd17 to your computer and use it in GitHub Desktop.
python time on reduce vs Counter
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 random | |
| import timeit | |
| from functools import reduce | |
| from collections import Counter | |
| data = [] | |
| for i in range(0,1000000): | |
| data.append({'team': random.choice("ABCDEFGHIJKLMNO"), 'score': random.randint(1,10000)}) | |
| def compute(acc, i): | |
| acc[i['team']]=acc.get(i['team'], 0) + i['score'] | |
| return acc | |
| def f1(d): | |
| """ | |
| Counter | |
| """ | |
| c = Counter() | |
| for i in d: | |
| c[i['team']]+=i['score'] | |
| return c | |
| def f2(d): | |
| """ | |
| reduce + lambda + compute func | |
| """ | |
| return reduce(lambda acc, i: compute(acc, i), d, {}) | |
| def f3(d): | |
| """ | |
| reduce + lambda + update | |
| """ | |
| return reduce(lambda acc, i: (acc.update({i['team']: acc.get(i['team'], 0) + i['score']}) or acc), d, {}) | |
| def f4(d): | |
| """ | |
| reduce + lambda + setitem | |
| """ | |
| return reduce(lambda acc, i: (acc.__setitem__(i['team'], acc.get(i['team'], 0) + i['score']) or acc), d, {}) | |
| fs = [f1, f2, f3, f4] | |
| def test_groupby_sum(f): | |
| out = f(data) | |
| for k, v in out.items(): | |
| print(f"{k}={v} ", end="") | |
| for f in fs: | |
| elapsed = timeit.timeit(lambda x=f: test_groupby_sum(x), number = 5) | |
| print(f"{f.__doc__}: {elapsed:.6f}s\n") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment