I’m working on a stochastic model which heavily relies on random sampling from a list of items. Normally I turn to NumPy for this with its
numpy.random.choice() function. To my surprise, it turned out that
random.choice()) in Python’s standard library is quite a bit faster:
import numpy as np x = np.arange(1000) %timeit np.random.choice(x, replace=False)
gives me 16.2 µs ± 264 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each), while:
import random x = list(range(1000)) %timeit random.sample(x, 1)
gives me 2.16 µs ± 32.6 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each).
Anyone an idea of what might be going on?