简体   繁体   English

重用SystemRandom是一种很好的Python实践吗?

[英]Is it good Python practice to reuse SystemRandom?

For example, a block of Python code to generate 1 million random strings off the alphabet 'abcdef'. 例如,一段Python代码,用于在字母“abcdef”中生成100万个随机字符串。

Compare 相比

from random import SystemRandom

for _ in range(1000000):
    ''.join(SystemRandom().choice('abcdef') for __ in range(5))

with

from random import SystemRandom

r = SystemRandom()
for _ in range(1000000):
    ''.join(r.choice('abcdef') for __ in range(5))

I timed it on IPython. 我在IPython上计时了。 The 1st block took 50s while the 2nd block took 30s. 第一个区块耗时50秒,而第二个区块耗时30秒。 I didn't measure the memory usage. 我没有测量内存使用情况。

In Java, there is SecureRandom where the pattern is clear that it should be created once and reused. 在Java中,存在SecureRandom,其中模式清楚,应该创建一次并重用。 Coming from Java to Python, it is not clear to me whether I should create a single SystemRandom instance and reuse it. 从Java到Python,我不清楚是否应该创建一个SystemRandom实例并重用它。 The 1st block is easier to read and the performance does not seem too bad. 第一个块更容易阅读,性能似乎并不太糟糕。

In general, does Python programming pay more attention to readability than performance? 一般来说,Python编程是否更注重可读性而不是性能?

SystemRandom builds on the platform C's urandom() function, which is as secure as your operating system developers knew how to make it. SystemRandom构建于平台C的urandom()函数之上,该函数与您的操作系统开发人员知道如何制作一样安全。 Virtually everyone uses a single instance of it (as in your second example). 实际上每个人都使用它的一个实例(如第二个例子)。

There's no real conflict between performance and readability here. 这里的性能和可读性之间没有真正的冲突。 Idiomatic Python would be like: 惯用Python就像:

from random import SystemRandom
r = SystemRandom()
choice = r.choice

for _ in range(1000000):
    ''.join(choice('abcdef') for _ in range(5))

When in doubt, I usually look at the implementation ... 如有疑问,我通常会查看实施情况 ......

As far as I can tell, SystemRandom works pretty much exactly the same as Random ( which is reused by all of the module-level functions ) except that the random method is swapped out to call os.urandom . 据我所知, SystemRandomRandom所有模块级函数重用 )完全相同,只是random方法被换出来调用os.urandom So, as far as I can tell, you should be able to re-use a SystemRandom instance. 因此,据我所知,您应该能够重用SystemRandom实例。

In general, does Python programming pay more attention to readability than performance? 一般来说,Python编程是否更注重可读性而不是性能?

In general, I think that the mentality is that readability counts and code is read more often than written . 一般来说,我认为心态是可读性很重要,而且代码的读取频率高于写入 You should only really spend time (or code complexity) optimizing things which are taking a significant amount of your over-all program running time. 你应该只花时间(或代码复杂性)优化那些占用大量整个程序运行时间的东西。 If creating and calling SecureRandom takes a tiny percentage of your overall program running time, then it's probably not worth optimizing and you should definitely go with the more easy to read version. 如果创建和调用SecureRandom占用整个程序运行时间的一小部分,那么它可能不值得优化,你应该选择更容易阅读的版本。 If it takes 50% of the run-time and waiting for the program to complete is annoying, you might want to look into optimizing. 如果需要50%的运行时间并且等待程序完成很烦人,那么您可能需要考虑优化。

Of course, with that said, I don't think there's much of a readability difference between your two code snippets, so ... I'd probably create one and reuse it... 当然,话虽如此,我认为你的两个代码片段之间的可读性差异不大,所以...我可能会创建一个并重用它...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM