ryd994

2017-10-01 02:39:57 +08:00

可以对比一下 choice 和 choices 的源码

https://hg.python.org/cpython/file/tip/Lib/random.py#l252

https://hg.python.org/cpython/file/tip/Lib/random.py#l340

choice 是生成一个随机的整数索引

choices 是把分布比重(默认等比重)转换成 0-1 的数轴,然后 random()生成 0-1 小数,对应到数轴上

大家底层都是用的 random(),choices 更复杂,理应更慢才对

使用 cProfile 测试

>>> cProfile.run('"".join(random.choice(string.ascii_letters + string.digits) for _ in range(10**7))')

60321941 function calls in 21.869 seconds

Ordered by: standard name

ncalls tottime percall cumtime percall filename:lineno(function)

10000001 5.516 0.000 20.772 0.000 :1()

1 0.000 0.000 21.869 21.869 :1()

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)

1 0.000 0.000 21.869 21.869 {built-in method builtins.exec}

10000000 0.956 0.000 0.956 0.000 {built-in method builtins.len}

10000000 0.785 0.000 0.785 0.000 {method 'bit_length' of 'int' objects}

1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}

10321936 1.851 0.000 1.851 0.000 {method 'getrandbits' of '_random.Random' objects}

1 1.097 1.097 21.869 21.869 {method 'join' of 'str' objects}

>>> cProfile.run('"".join(random.choices(string.ascii_letters + string.digits, k=10**7))')

10000007 function calls in 3.463 seconds

Ordered by: standard name

ncalls tottime percall cumtime percall filename:lineno(function)

1 0.014 0.014 3.463 3.463 :1()

1 0.000 0.000 3.374 3.374 random.py:340(choices)

1 2.780 2.780 3.374 3.374 random.py:352()

1 0.000 0.000 3.463 3.463 {built-in method builtins.exec}

1 0.000 0.000 0.000 0.000 {built-in method builtins.len}

1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}

1 0.075 0.075 0.075 0.075 {method 'join' of 'str' objects}

10000000 0.594 0.000 0.594 0.000 {method 'random' of '_random.Random' objects}

可以看到:

1. choice 法到底层用的是 getrandbits

# Only call self.getrandbits if the original random() builtin method

# has not been overridden or if a new getrandbits() was supplied.

说明 getrandbits 应该是比 random 更快的,否则官方不会这么用

2. choice 法的 function calls 是 choices 法的 6 倍,而正好时间也是将近 6 倍,很可能这两者是有关联的

3.看 tottime,choices 的时间主要是在 random.py:352

return [population[_int(random() * total)] for i in range(k)]

这里构建 list 消耗大可以理解

choice 的时间主要是在:1,random.py:222,random.py:252 上

choice 一个 5 行的函数,吃这么多时间,很难理解

python随机生成字符串_Python 生成一段随机字符串的两种写法相关推荐

  1. python训练自己中文语料库_Python nltk载入自己的中文语料库的两种方法 for Windows7...

    前提:把自己的语料库(sogou文本分类语料库)放在LTK_DATA/corpora/目录下: 然后在命令行输入以下之后,即可看到所有的txt文件名列表了. 第一种方法:BracketParseCor ...

  2. python装饰器带参数函数_python带参数装饰器的两种写法

    python带参数装饰器的两种写法 前言 最近在实现一个装饰器的过程中发现了一个很有意思的地方,在博客里面分享出来 不同的写法 三层函数嵌套,实现了可传参数的一个装饰器. import logging ...

  3. 5.4 一家人才测评机构低随机抽取的10名小企业的经理人用两种方法进行自信心测试,得到的自信心测试分数如下

    2022-11-29 5.4 一家人才测评机构低随机抽取的10名小企业的经理人用两种方法进行自信心测试,得到的自信心测试分数如下 人员编号 方法1 方法2 1 78 71 2 63 44 3 72 6 ...

  4. python随机生成字符串_python生成随机数、随机字符串

    python生成随机数.随机字符串 import random import string # 随机整数: print random.randint(1,50) # 随机选取0到100间的偶数: pr ...

  5. python 随机字符串_python生成随机数、随机字符串

    python生成随机数.随机字符串 import random import string # 随机整数: print random.randint(1,50) # 随机选取0到100间的偶数: pr ...

  6. python生成10个随机数字符串_python生成随机数、随机字符串

    python生成随机数.随机字符串 import random import string # 随机整数: print random.randint(1,50) # 随机选取0到100间的偶数: pr ...

  7. python获取随机字符串_python生成随机字符串

    方法一,大小写字母+数字: import random import string ran_str = ''.join(random.sample(string.ascii_letters + str ...

  8. python 生成随机数_python 生成随机数模块random 常用方法总结

    random.random() 用来随机生成一个0到1之间的浮点数,包括零. In [1]: import random In [2]: random.random() Out[2]: 0.15790 ...

  9. python创建数字列表_Python 生成一个从0到n个数字的列表4种方法小结

    Python 生成一个从0到n个数字的列表4种方法小结 我就废话不多说了,直接上代码吧! 第一种 def test1(): l = [] for i in range(1000): l = l + [ ...

  10. python设定数值范围_Python 生成周期性波动的数据 可指定数值范围

    代码 import numpy as np import math import matplotlib.pyplot as plt #python在指定的时间段生成周期性波动的数据: #周期性 lon ...

最新文章

  1. mysql 存储过程out,in,inout分别表示什么
  2. 2440裸机编程之四 外部中断
  3. BZOJ3336: Uva10572 Black and White(插头Dp)
  4. 学会了Python之后,我的职业生涯突飞猛进
  5. RSA openssl_public_encrypt false
  6. oracle ogg 12安装,Oracle GoldenGate Studio 12.2.1.3安装
  7. LeetCode For SQL 184. 部门工资最高的员工 (分组 from嵌套)
  8. ODBC数据源的作用及配置
  9. Matlab中的画图函数
  10. 2022年国内短信平台大全
  11. 数据结构PTA 进阶实验5-3.2 新浪微博热门话题(分离链接法 )
  12. JavaScript(JS)的基本语法
  13. 数学建模暑期集训24:机器学习与Classification Learner工具箱实操
  14. 两两相望计算机音乐,两两相望 苏汐洋 两两相望歌曲,两两相望mp3在线试听 - 5nd音乐网...
  15. Mac电脑的效率超高的输入法,自动切换输入法
  16. pandas之dropna()的用法
  17. Python turtle画图库画姓名实例(Python入门)
  18. 公司U13 资本成本 习题解读
  19. R语言-导入数据集并以第一列为行名
  20. 发那科机器人override指令_发那科机器人程序是如何编写的呢——发那科机器人...

热门文章

  1. python实现卷积神经网络_【455】Python 徒手实现 卷积神经网络 CNN
  2. SortedSet和TreeSet
  3. CentosRedhat下bcm43142博通无线网卡linux驱动之二
  4. ARM/IBM左右夹攻 英特尔服务器举步维艰?
  5. Zabbix安装界面显示PHP time zone 为“红色”的解决办法
  6. DeNA/上海纵游通过使用AWS大量缩短新款游戏和服务的上线时间
  7. 将java类的泛型集合转换成json对象
  8. linux定时任务的配置详解
  9. WCF去掉证书验证(转载)
  10. 企业在信息化建设上乘之选:软件快速开发框架