相关疑难解决方法(0)

Why is random.sample faster than numpy's random.choice?

I need a way to sample without replacement a certain array a. I tried two approaches (see MCVE below), using random.sample() and np.random.choice.

I assumed the numpy function would be faster, but it turns out it is not. In my tests random.sample is ~15% faster than np.random.choice.

Is this correct, or am I doing something wrong in my example below? If this is correct, why?

import numpy as np
import random
import time
from contextlib import contextmanager …
Run Code Online (Sandbox Code Playgroud)

python random numpy

13
推荐指数
1
解决办法
5334
查看次数

标签 统计

numpy ×1

python ×1

random ×1