I suspect that the np.random.choice community slows it down, especially for small samples than large ones.
Rough vectorization of the if version:
def foo(n): x = np.random.rand(n) var = np.zeros(n) var[x<.25] = -1 var[x>.75] = 1 return var
Running in ipython I get:
timeit np.random.choice([-1,0,1],size=1000,p=[.25,.5,.25]) 1000 loops, best of 3: 293 us per loop timeit foo(1000) 10000 loops, best of 3: 83.4 us per loop timeit np.random.choice([-1,0,1],size=100000,p=[.25,.5,.25]) 100 loops, best of 3: 11 ms per loop timeit foo(100000) 100 loops, best of 3: 8.12 ms per loop
So, for a size of 1000 choice ’s 3-4x slower, but with large vectors the difference begins to disappear.
source share