python timeit
//有时候,我们想知道一个函数的计算耗时,那么,你可以用timeit
//test.py
1 import timeit
2
3 def func():
4 s = 0
5 for i in range(1000):
6 s += i
7 pass
8
9 def test_range(n):
10 for i in range(n):
11 pass
12
13 def test_xrange(n):
14 for i in xrange(n):
15 pass
16 t = timeit.timeit('test_range(100)', 'from __main__ import test_range', number=1000)
17 print(t)
18 t = timeit.timeit('test_range(100)', 'from __main__ import test_range', number=10000)
19 print(t)
20 t = timeit.timeit('test_xrange(100)', 'from __main__ import test_xrange', number=10000)
21 print (t)
//result
# python test.py
0.00116419792175
0.0125250816345
0.0108740329742
Finally:
xrange比range要快
python timeit的更多相关文章
- [Python] timeit测试代码运行效率
python中有两种方法判断一个数是不是偶数或者奇数: In [29]: 3&1 Out[29]: 1 In [30]: 3%2 Out[30]: 1 In [31]: 4&1 Out ...
- 如何使用python timeit模块使用实践
其实平时使用测试应用运行时间的情况 细算一下还真的很少.很久没有做性能优化的工作,不管是cProfile还是timeit模块都已经生疏了很久没有使用,我在以前的文章里面有提到过cPfile的性能测试使 ...
- python timeit模块用法
想测试一行代码的运行时间,在python中比较方便,可以直接使用timeit: >>> import timeit #执行命令 >>> t2 = timeit.Ti ...
- python timeit模块
timeit模块timeit模块可以用来测试一小段Python代码的执行速度. class timeit.Timer(stmt='pass', setup='pass', timer=<time ...
- python timeit模块简单用法
timeit模块提供了一种简便的方法来为Python中的小块代码进行计时. 模块调用函数,stmp为要测试的函数,setup为测试环境,number为运行次数 timeit.timeit(stmt=) ...
- python性能分析(一)——使用timeit给你的程序打个表吧
前言 我们可以通过查看程序核心算法的代码,得知核心算法的渐进上界或者下界,从而大概估计出程序在运行时的效率,但是这并不够直观,也不一定十分靠谱(在整体程序中仍有一些不可忽略的运行细节在估计时被忽略了) ...
- python profile
一.profile,cProfile 1. python -m cProfile myprogram.py python -m profile myprog.py2. 使用import profile ...
- [python]用profile协助程序性能优化
转自:http://blog.csdn.net/gzlaiyonghao/article/details/1483728 本文最初发表于恋花蝶的博客http://blog.csdn.net/lanph ...
- Python学习笔记011_模块_标准库_第三方库的安装
容器 -> 数据的封装 函数 -> 语句的封装 类 -> 方法和属性的封装 模块 -> 模块就是程序 , 保存每个.py文件 # 创建了一个hello.py的文件,它的内容如下 ...
随机推荐
- mtd工具
http://daemons.net/linux/storage/mtd.html MTD The Memory Technology Devices (MTD) subsystem provides ...
- np.mgird np.ogrid
np.ogrid: address:https://docs.scipy.org/doc/numpy/reference/generated/numpy.ogrid.html returns an o ...
- <转>记dynamic的一个小坑 -- RuntimeBinderException:“object”未包含“xxx”的定义
→转载地址← 创建一个控制台程序和一个类库, 在控制台创建一个匿名对象,然后再在类库中访问它,代码如下: namespace ConsoleApplication1 { class Program { ...
- 用U盘制作并安装WIN10 64位原版系统的详细教程(该方法应该适用于任何一版的原版操作系统)
https://www.cnblogs.com/Jerseyblog/p/6518273.html
- 如何写好.babelrc?Babel的presets和plugins配置解析
什么是Babel The compiler for writing next generation JavaScript. 官网是这么说的,翻译一下就是下一代JavaScript 语法的编译器. 作为 ...
- day3_字典
一.说明 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示: dict = {key1:value1,key2 ...
- adg的数据传输应用三大模式转换
1.最大可用性模式(Maximum Availability) 1)该模式提供了仅次于"最大保护模式"的数据保护能力: 2)要求至少一个物理备库收到重做日志后,主库的事务才能够提交 ...
- telnet测试端口的使用
端口开启后会跳转到 :
- js判断开始时间不能小于结束时间
function validTime(startTime,endTime){ var arr1 = startTime.split("-"); var arr2 = e ...
- docker+jenkins+maven简单部署
构建jar包 1.拉取jenkins容器景象 docker pull docker.io/jenkins/jenkins 2.配置映射目录,创建一个容器 mkdir /data/jenkins doc ...