chace's recent timeline updates
chace

chace

V2EX member #165388, joined on 2016-03-29 20:52:28 +08:00
chace's recent replies
牛🐮 大佬有兴趣帮忙实现一个 Flutter 组件吗?有偿💰

vx: emhvdWNoYW85NDIwCg==
Oct 1, 2017
Replied to a topic by 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 速度更快。
Oct 1, 2017
Replied to a topic by chace Python Python 生成一段随机字符串的两种写法
@workwonder 中间一段代码都是在生成 random.seed()。可能是为了随机效果更好吧。
Oct 1, 2017
Replied to a topic by 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}
Sep 30, 2017
Replied to a topic by chace Python Python 生成一段随机字符串的两种写法
@Daniel65536 这个内置函数的速度是最快的,100W 个字符 3.15765118598938s
Sep 30, 2017
Replied to a topic by chace Python Python 生成一段随机字符串的两种写法
@Daniel65536 谢谢。目测跟我一样少打了一个括号^_^
Sep 30, 2017
Replied to a topic by chace Python Python 生成一段随机字符串的两种写法
@jingniao 没注意,谢谢提醒
About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   981 Online   Highest 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 14ms · UTC 22:06 · PVG 06:06 · LAX 15:06 · JFK 18:06
♥ Do have faith in what you're doing.