Skip to content
Advertisement

Is there a faster version of numpy.random.shuffle?

I’m using numpy.random.shuffle in order to compute a statistic on randomized columns of a 2D array. The Python code is as follows:

JavaScript

The speed I get is something like this:

JavaScript

JavaScript
1 loops, best of 3: 391 ms per loop

I tried to Cythonize this function but I wasn’t sure how to replace the call to np.random.shuffle and the function was 3x slower. Does anyone know how to accelerate or replace this? It is currently the bottleneck in my program.

Cython code:

JavaScript

Advertisement

Answer

It’s likely that this will give a nice speed boost:

JavaScript

Forcing it to be contiguous does increase the time, but not by a ton:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement