In [1]:
import numpy as np
from collections import defaultdict, Counter
In [2]:
A_np = np.random.randint(0, 10 ** 4, 10 ** 6)
A_li = list(A_np)
In [3]:
%%timeit
d = defaultdict(int)
for a in A_li:
    d[a] += 1
290 ms ± 2.73 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [4]:
%%timeit
Counter(A_li)
195 ms ± 3.23 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [5]:
%%timeit
np.unique(A_np, return_counts = True)
105 ms ± 508 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [6]:
%%timeit
np.bincount(A_np)
8.07 ms ± 130 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)