在python中获取介于1到1百万之间的随机数的最快方法 [英] Fastest way to get a random number between 1 and 1million in python

查看:519
本文介绍了在python中获取介于1到1百万之间的随机数的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实现这一目标的最快最快方法是什么?我每天将做超过一百万次,所以我想要最大的效率.

What is the absolute fastest way of achieving this? I will be doing this over 1 million times per day so I want maximum efficiency.

使用numpy(20次运行后平均0.0001679009692429128)

With numpy (avg after 20 runs 0.0001679009692429128)

t0 = time.clock()
print(np.random.randint(1,1000000))
t1 = time.clock()
print (t1-t0)

随机(平均:0.0000920492372555262)

With random (avg: 0.0000920492372555262)

t2 = time.clock()
print(random.choice(range(1,1000000)))
t3 = time.clock()
print (t3-t2)

令我惊讶的是,随机数始终比numpy快.有没有更快的方法?

To my surprise random was consistently faster than numpy. Is there a faster way?

推荐答案

numpy在生成大型随机数样本(数组)时效率更高.例如,

numpy is more efficient when generating large samples (arrays) of random numbers. For example,

In [10]: %timeit np.random.randint(1,1000000, 1000000)
5.14 ms ± 64.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [11]: %timeit [random.choice(range(1,1000000)) for _ in range(1000000)]
1.01 s ± 14.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

此外,请参见如何为使用Python的timeit测试性能的代码段计时?有关如何执行计时的信息测试.使用time.clock()时,至少应尝试多次重复该操作,然后计算平均时间.最好使用timeit进行时序测试.另外,正如其他人在评论中提到的那样,print()花费的时间比随机数生成要长得多,因此您的计时测试主要是测量print()的运行速度.相反,您应该执行以下操作:

In addition, see How can I time a code segment for testing performance with Pythons timeit? on how to perform timing tests. When you use time.clock(), you should at least try to repeat the operation multiple times and then compute mean time. It is more advisable to use timeit for timing tests. Also, as others have mentioned in the comments, print() takes significantly longer that random number generation, so your timing test is mostly measuring how fast print() works. Instead, you should do something like this:

In [12]: repeat = 1000000
    ...: t0 = time.clock()
    ...: for _ in range(repeat):
    ...:     np.random.randint(1, 1000000)
    ...: t1 = time.clock()
    ...: print((t1 - t0) / repeat)
1.3564629999999908e-06

In [13]: repeat = 1000000
    ...: t2 = time.clock()
    ...: for _ in range(repeat):
    ...:     random.choice(range(1, 1000000))
    ...: t3 = time.clock()
    ...: print((t3 - t2) / repeat)
1.0206699999999956e-06

因此,对于单个数字,numpy平均比内置随机数生成器慢大约35%.但是,以前的测试表明,生成大型样本时,numpy的速度明显更快.

So, for a single number, numpy is on average just about 35% slower than built-in random number generator. However, previous tests show that when generating large samples, numpy is significantly faster.

这篇关于在python中获取介于1到1百万之间的随机数的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆