chace 最近的时间轴更新
chace

chace

V2EX 第 165388 号会员,加入于 2016-03-29 20:52:28 +08:00
chace 最近回复了
2017-10-01 11:46:26 +08:00
回复了 chace 创建的主题 Python Python 生成一段随机字符串的两种写法
@ryd994
应该是因为<string>:1,random.py:222random.py:252 都是 py 实现的,而且调用次数都是最多的,所以耗时吧。
choice:
10000001 5.516 0.000 20.772 0.000 <string>:1(<genexpr>)
10000000 6.283 0.000 8.918 0.000 random.py:222(_randbelow)
10000000 5.381 0.000 15.256 0.000 random.py:252(choice)

而在 choices 中,调用最多的是 C 语言实现的,所以不耗时。
choices:
10000000 0.594 0.000 0.594 0.000 {method 'random' of '_random.Random' objects}

楼上有 XD 提到 os.urandom()调用 syscall(such as /dev/urandom on Unix or CryptGenRandom on Windows)生成一段随机的 bytes 速度更快。
2017-10-01 11:23:56 +08:00
回复了 chace 创建的主题 Python Python 生成一段随机字符串的两种写法
@workwonder 中间一段代码都是在生成 random.seed()。可能是为了随机效果更好吧。
2017-10-01 11:22:43 +08:00
回复了 chace 创建的主题 Python Python 生成一段随机字符串的两种写法
@happlebao
厉害,还有这个方法
>>> cProfile.run('binascii.hexlify(os.urandom(10**7)).decode()')
6 function calls in 0.789 seconds

Ordered by: standard name

ncalls tottime percall cumtime percall filename:lineno(function)
1 0.005 0.005 0.789 0.789 <string>:1(<module>)
1 0.032 0.032 0.032 0.032 {built-in method binascii.hexlify}
1 0.000 0.000 0.789 0.789 {built-in method builtins.exec}
1 0.747 0.747 0.747 0.747 {built-in method posix.urandom}
1 0.005 0.005 0.005 0.005 {method 'decode' of 'bytes' objects}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
2017-09-30 22:34:02 +08:00
回复了 chace 创建的主题 Python Python 生成一段随机字符串的两种写法
@Daniel65536 这个内置函数的速度是最快的,100W 个字符 3.15765118598938s
2017-09-30 22:18:15 +08:00
回复了 chace 创建的主题 Python Python 生成一段随机字符串的两种写法
@Daniel65536 谢谢。目测跟我一样少打了一个括号^_^
2017-09-30 22:16:18 +08:00
回复了 chace 创建的主题 Python Python 生成一段随机字符串的两种写法
@jingniao 没注意,谢谢提醒
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1519 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 13ms · UTC 17:17 · PVG 01:17 · LAX 10:17 · JFK 13:17
Developed with CodeLauncher
♥ Do have faith in what you're doing.