numpy.random.normal() 在多线程环境中
numpy.random.normal() in multithreaded environment
我试图用 numpy.random.normal() 函数并行化正态分布的随机数组的生成,但看起来来自线程的调用是顺序执行的。
import numpy as np
start = time.time()
active_threads = 0
for i in range(100000):
t = threading.Thread(target=lambda x : np.random.normal(0,2,4000), args = [0])
t.start()
while active_threads >= 12:
time.sleep(0.1)
continue
end = time.time()
print(str(end-start))
如果我测量 1 个线程进程的时间,我得到与 12 个线程进程相同的结果。
我知道这种并行化会带来很多开销,但即便如此,多线程版本仍然需要一些时间。
我试图用 numpy.random.normal() 函数并行化正态分布的随机数组的生成,但看起来来自线程的调用是顺序执行的。
import numpy as np
start = time.time()
active_threads = 0
for i in range(100000):
t = threading.Thread(target=lambda x : np.random.normal(0,2,4000), args = [0])
t.start()
while active_threads >= 12:
time.sleep(0.1)
continue
end = time.time()
print(str(end-start))
如果我测量 1 个线程进程的时间,我得到与 12 个线程进程相同的结果。 我知道这种并行化会带来很多开销,但即便如此,多线程版本仍然需要一些时间。