[英]Fastest way to fill numpy array with random numbers
Is there a faster way to get a numpy array filled with random numbers than the built in numpy.random.rand(count)
function? 是否有更快的方法来获得一个充满随机数的numpy数组而不是内置的numpy.random.rand(count)
函数? I know that the built in method is using the Mersenne Twister. 我知道内置方法是使用Mersenne Twister。
I would like to use numpy for monte carlo simulations, and fetching the random numbers is taking a significant portion of the time. 我想使用numpy进行蒙特卡罗模拟,并且获取随机数是占用大部分时间的。 A simple example, calculating pi by monte carlo integration with 200E6 random numbers is only processing about 116.8 MB/s through my program. 一个简单的例子,通过monte carlo与200E6随机数进行积分计算pi只能通过我的程序处理大约116.8 MB / s。 A comprable program written in C++ using xor128() as the generator processes several hundred MB/s. 使用xor128()作为生成器以C ++编写的可编译程序处理数百MB / s。
EDIT: Miscalculated generation rate 编辑:计算的生成率
You could perhaps get a slight increase in performance by reducing the accuracy - if this is acceptable. 如果可以接受,你可能会通过降低准确性来略微提高性能。 I did this by using randint
and scaling: 我通过使用randint
和缩放来做到这一点:
Using ipython %%timeit
使用ipython %%timeit
count =1000
numpy.random.rand(count)
10000 loops, best of 3: 24.3us per loop
numpy.random.randint(0,1000,count)*0.001
10000 loops, best of 3: 21.4us per loop
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.