Search code examples
pythonperformancerandomnumpy-random

Why does np.random.choice take so much longer than np.random.randint?


I've been reading that np.random.choice is faster than np.random.randint (here and here), however when comparing the speed difference between

import numpy as np

for i in range(100000):
    np.random.choice(100)

and

import numpy as np

for i in range(100000):
    np.random.randint(100)

np.random.randint is appreciably faster. What's the difference between these two?


Solution

  • Both of the posts you mentioned are about the standard library random and you are using the 3rd-party library numpy. They are completely different.

    In addition, numpy is not good at generating one element at a time like that. Use random (standard library), or generate them in bulk as follows.

    np.random.randint(0, 100, size=100000)
    
    np.random.choice(100, size=100000)
    

    This makes the performance of both almost same.