//有时候,我们想知道一个函数的计算耗时,那么,你可以用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的更多相关文章

  1. [Python] timeit测试代码运行效率

    python中有两种方法判断一个数是不是偶数或者奇数: In [29]: 3&1 Out[29]: 1 In [30]: 3%2 Out[30]: 1 In [31]: 4&1 Out ...

  2. 如何使用python timeit模块使用实践

    其实平时使用测试应用运行时间的情况 细算一下还真的很少.很久没有做性能优化的工作,不管是cProfile还是timeit模块都已经生疏了很久没有使用,我在以前的文章里面有提到过cPfile的性能测试使 ...

  3. python timeit模块用法

    想测试一行代码的运行时间,在python中比较方便,可以直接使用timeit: >>> import timeit #执行命令 >>> t2 = timeit.Ti ...

  4. python timeit模块

    timeit模块timeit模块可以用来测试一小段Python代码的执行速度. class timeit.Timer(stmt='pass', setup='pass', timer=<time ...

  5. python timeit模块简单用法

    timeit模块提供了一种简便的方法来为Python中的小块代码进行计时. 模块调用函数,stmp为要测试的函数,setup为测试环境,number为运行次数 timeit.timeit(stmt=) ...

  6. python性能分析(一)——使用timeit给你的程序打个表吧

    前言 我们可以通过查看程序核心算法的代码,得知核心算法的渐进上界或者下界,从而大概估计出程序在运行时的效率,但是这并不够直观,也不一定十分靠谱(在整体程序中仍有一些不可忽略的运行细节在估计时被忽略了) ...

  7. python profile

    一.profile,cProfile 1. python -m cProfile myprogram.py python -m profile myprog.py2. 使用import profile ...

  8. [python]用profile协助程序性能优化

    转自:http://blog.csdn.net/gzlaiyonghao/article/details/1483728 本文最初发表于恋花蝶的博客http://blog.csdn.net/lanph ...

  9. Python学习笔记011_模块_标准库_第三方库的安装

    容器 -> 数据的封装 函数 -> 语句的封装 类 -> 方法和属性的封装 模块 -> 模块就是程序 , 保存每个.py文件 # 创建了一个hello.py的文件,它的内容如下 ...

随机推荐

  1. hashMap 和linkedHashMap

    hashMap是个单向链表的数组 linkedHashMap是个双向链表的数组,modal就是linkedHashMap

  2. win10 安装 open live write

    安装完 open live write后将Memento.OLW_V1.0.0.3.7z解压到C:\Users\pc_name\AppData\Local\OpenLiveWriter\app-0.6 ...

  3. CodeForces 1099E - Nice table - [好题]

    题目链接:https://codeforces.com/problemset/problem/1099/E You are given an $n×m$ table, consisting of ch ...

  4. 安装和配置hive

    1.上传hive.mysql.mysql driver到服务器/mnt目录下: [root@chavin mnt]# ll mysql-5.6.24-linux-glibc2.5-x86_64.tar ...

  5. 关于Java程序流程控制的整理(未完善)

  6. jcgrid

    1.jcgrid实现 <span class="JcGrid" id="MyGrid" listdata="Rds.List.UserList& ...

  7. SHOW PROCESSLIST shows which threads are running 查看线程 解决瓶颈

    小结: 1.查看全部线程: https://dev.mysql.com/doc/refman/8.0/en/show-processlist.html MySQL 8.0 Reference Manu ...

  8. php值callback类型和匿名函数(闭包)

    callback.callable类型 自PHP5.4起可以使用callable类型制定回调类型callback. 本文档基于同样理由使用callback类型信息. 一些函数如call_user_fu ...

  9. Chap5:数字货币交易[《区块链中文词典》维京&甲子]

  10. <大话设计模式>工厂模式,策略模式

    第一章:工厂模式: 通过封装,继承,多态解耦合 业务逻辑和界面逻辑分开 用单独的类创造实例,工厂:创造实例 工厂模式还可以用反射来实现,nsstringFromClass UML类图 聚合表示一众弱的 ...