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的文件,它的内容如下 ...
随机推荐
- JavaScript----特效代码
1.不同时间段显示不同问候语 <Script Language="JavaScript"> <!-- var text=""; day = n ...
- Jenkins设置备份
安装备份插件,系统管理-插件管理 可选插件搜索backup 备份 系统管理-Backup manager 设置备份路径 恢复
- Javascript扩展Date的prototype实现时间format函数 2017-06-29T09:10:00.000Z日期转换
/*时间格式化 公用方法*/ Date.prototype.format = function(fmt) { // var o = { "M+": this.getMonth() ...
- MapReduce 找出共同好友
这个前提需要注意:好友之间的关系是单向的,我的好友队列里有你,你的里面不一定有我.所以思考方式需要改变. 共同好友: 某两个人的好友队列里都有的人. 第一个mapper 和 reducer 简单说:找 ...
- Codeforces 660C - Hard Process - [二分+DP]
题目链接:http://codeforces.com/problemset/problem/660/C 题意: 给你一个长度为 $n$ 的 $01$ 串 $a$,记 $f(a)$ 表示其中最长的一段连 ...
- 计蒜客 31436 - 提高水平 - [状压DP]
题目链接:https://nanti.jisuanke.com/t/31436 作为一名车手,为了提高自身的姿势水平,平时的练习是必不可少的.小 J 每天的训练包含 $N$ 个训练项目,他会按照某个顺 ...
- Java元组Tuple介绍与使用
一.元组介绍 仅仅一次方法调用就可以返回多个对象,你应该经常需要这样的功能吧.可以return语句只允许返回单个对(可能有人说返回一个集合就可以了,请记住,一个集合也只是一个对象而已)因此,解决办法就 ...
- LU decomposition can be viewed as the matrix form of Gaussian elimination.
https://en.wikipedia.org/wiki/LU_decomposition One way to find the LU decomposition of this simple m ...
- minikube k8 ingress--https://kubernetes.io/docs
https://ehlxr.me/2018/01/12/kubernetes-minikube-installation/[Kubernetes 学习笔记之 MiniKube 安装 in CHINA] ...
- kvm qemu ,ubuntu debian rootfs 通过qemu复现路由器漏洞
KVM https://www.toutiao.com/i6264303973256135170/?tt_from=weixin&utm_campaign=client_share&t ...