我注意到,尝试加速涉及通过将python for
循环向量化来生成大量随机数的numpy代码可能会产生相反的结果并且可能会降低它的速度。以下代码的输出为:took time 0.588
和took time 0.789
。这违背了我对如何最好地编写numpy代码的直觉,我想知道为什么会出现这种情况?
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
答案 0 :(得分:0)
IMO你的同伴不太公平 - 从一维阵列列表中测量建立二维阵列的时间怎么样?
In [127]: %timeit np.random.randint(0,N,size=(N,M))
1.32 s ± 24.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [128]: %timeit np.column_stack(np.random.randint(0,N,size=N) for _ in range(M))
2.73 s ± 135 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)