Numpy随机数生成在矢量化后运行较慢

时间:2018-01-14 16:13:01

标签: python python-2.7 numpy random

我注意到,尝试加速涉及通过将python for循环向量化来生成大量随机数的numpy代码可能会产生相反的结果并且可能会降低它的速度。以下代码的输出为:took time 0.588took 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

1 个答案:

答案 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)