I noticed that trying to speed up numpy code, which involves generating a large number of random numbers by vectorizing python loops for, may have the opposite result and may slow it down. Output of the next bit of code: took time 0.588and took time 0.789. This runs counter to my intuition about how best to write numpy code, and I was wondering why this would be so?
import time
import numpy as np
N = 50000
M = 1000
repeats = 10
start = time.time()
for i in range(repeats):
for j in range(M):
r = np.random.randint(0,N,size=N)
print 'took time ',(time.time()-start)/repeats
start = time.time()
for i in range(repeats):
r = np.random.randint(0,N,size=(N,M))
print 'took time ',(time.time()-start)/repeats
source
share