最早见过手写的,类似于下面这种:

1 import datetime

2

3 def time_1():

4 begin = datetime.datetime.now()

5 sum = 0

6 for i in xrange(10000000):

7 sum = sum + i

8 end = datetime.datetime.now()

9 return end-begin

10

11 print time_1()

输出如下:

➜ Python python time_1.py

0:00:00.280797

另外一种方法是使用timeit模块,使用方法如下:

In [5]: import timeit

In [6]: timeit.timeit("sum(range(100))")

Out[6]: 1.2272648811340332

还可以在命令行上使用这种timeit模块,如下:

➜ Python python -m timeit -s"import time_1 as t" "t.time_1()"

0:00:00.282044

10 loops, best of 3: 279 msec per loop

注意:timeit模块会多次运行程序以获得更精确的时间,所以需要避免重复执行带来的影响。比方说x.sort()这种操作,因为第一次执行之后,后边已经是排好的了,准确性就收到了影响。

还有一种方法是使用cProfile模块,代码如下,名字为time_1.py:

1 import datetime

2

3 def time_1():

4 begin = datetime.datetime.now()

5 sum = 0

6 for i in xrange(10000000):

7 sum = sum + i

8 end = datetime.datetime.now()

9 return end-begin

10

11 if __name__ == '__main__':

12 print time_1()

13

14 import cProfile

15 cProfile.run('time_1()')

运行程序结果如下:

➜ Python python time_1.py

0:00:00.282828

2 function calls in 0.000 seconds

Ordered by: standard name

ncalls tottime percall cumtime percall filename:lineno(function)

1 0.000 0.000 0.000 0.000 :1()

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

Traceback (most recent call last):

File "time_1.py", line 15, in

cProfile.run('main()')

File "/usr/lib/python2.7/cProfile.py", line 29, in run

prof = prof.run(statement)

File "/usr/lib/python2.7/cProfile.py", line 135, in run

return self.runctx(cmd, dict, dict)

File "/usr/lib/python2.7/cProfile.py", line 140, in runctx

exec cmd in globals, locals

File "", line 1, in

NameError: name 'main' is not defined

➜ Python vi time_1.py

➜ Python python time_1.py

0:00:00.284642

5 function calls in 0.281 seconds

Ordered by: standard name

ncalls tottime percall cumtime percall filename:lineno(function)

1 0.000 0.000 0.281 0.281 :1()

1 0.281 0.281 0.281 0.281 time_1.py:3(time_1)

2 0.000 0.000 0.000 0.000 {built-in method now}

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

一开始代码里最后一行写的是cProfile.run('main()'),提示没有main(),将main()改成函数名字就可以了

这里是最简单的应用,具体大家可以去看看文档,或者直接help(xxx)

好物、羊毛线报群,需要的可加QQ群 1049623906

python输出运行时间_分析python程序运行时间的几种方法相关推荐

  1. python输出方格_使用Python实现LBM(格子法)方腔驱动流

    使用Python实现LBM(格子法)方腔驱动流 Python的不足: Python的最大优势也可能是它最大的弱点:它的灵活性和无类型的高级语法可能导致数据和计算密集型程序的性能不佳.-- 动态类型化解 ...

  2. python输出矩阵的转置_Python 矩阵转置的几种方法小结

    我就废话不多说了,直接上代码吧! #Python的matrix转置 matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] def printmatrix(m): fo ...

  3. mysql解析运行时间_分析 MySQL 语句运行时间

    为了验证select 1 与 select 1 from tableName 与 select * from tableName的执行效率,需要测试一下各自执行的时间.于是总结一下,查看mysql语句 ...

  4. python切片原理_分析python切片原理和方法

    使用索引获取列表的元素(随机读取) 列表元素支持用索引访问,正向索引从0开始 colors=["red","blue","green"] c ...

  5. python中输出菱形_用python打印菱形的实操方法和代码

    python怎么打印菱形?下面给大家带来三种方法: 第一种 rows = int(input('请输入菱形边长:\n')) row = 1 while row <= rows: col = 1 ...

  6. python 多核并行计算_嫌Python太慢?并行运算Process Pools三行代码给你4倍提速!

    大数据文摘作品,转载要求见文末,作者 | Adam Geitgey,编译 | 元元.Lisa.Saint.Aileen. Python绝对是处理数据或者把重复任务自动化的绝佳编程语言.要抓取网页日志? ...

  7. python 概率分布模型_使用python的概率模型进行公司估值

    python 概率分布模型 Note from Towards Data Science's editors: While we allow independent authors to publis ...

  8. python 时间序列预测_使用Python进行动手时间序列预测

    python 时间序列预测 Time series analysis is the endeavor of extracting meaningful summary and statistical ...

  9. 为什么python这么慢_为啥 Python 运行速度这么慢 ?

    原标题:为啥 Python 运行速度这么慢 ? 作者:Anthony Shaw 是 Python 软件基金会成员和 Apache 基金会成员. 近来Python可谓人气骤升.这门编程语言用于开发运维( ...

最新文章

  1. myeclipse生成注册码,破解,不用下载任何文件,代码方式破解
  2. 从投票应用说起,功能才不是轻社交App的核心呢!
  3. boost::math::legendre_stieltjes用法的测试程序
  4. promise的状态以及api介绍_前端 api 请求缓存
  5. RocketMQ源码解析-PullConsumer取消息(2)
  6. linux基本命令与终端操作、linux命令英文全称解释、ls clear cd pwd cat touch cp rm rmdir mkdir mv file find grep sudo su
  7. Python+OpenCV:训练级联分类器(Cascade Classifier Training)
  8. 暗黑启示录bt版java下载,Transmission搭建BT下载服务器
  9. csrss.exe病毒的清除方法
  10. 集合论—集合的基本运算与主要算律
  11. Sql Server 使用T-Sql 面向工业领域中商品的进货、销售、库存和记账等业务开发一个数据库应用系统:某客户、供应商以及他们之间往来的商品如汽车、电器等,需有效地记录这些商品进货、库存等账目,
  12. 方向传感器新的替代方法详解
  13. oracle的ofs,WINDOWS+MSCS+ORACLE+OFS
  14. 大连海事大学计算机基础线上考试虚拟机环境配置
  15. 最新 955 不加班的公司名单(2022 版)
  16. 虚拟机CentOS7启动报错:Entering emergency mode
  17. Centos 安装zlib
  18. 天地图服务http转https报错
  19. 顺丰--Java丰桥接口整体封装(下单、查询、打印)
  20. 【展馆设计】互动多媒体投影有哪些实用价值

热门文章

  1. SQL数据库的使用:创建表结构
  2. posix and system V IPC
  3. JAVA中的观察者模式observer
  4. 如何写代码,才能越写越轻松?
  5. [AX]AX2012 C#使用IIS宿主AIF服务的一些问题
  6. 接口implements
  7. 声笔码和声笔数码单字效率分析
  8. 反编译C#的dll文件并修改,再重新生成dll
  9. 移动互联网消息推送原理:长连接+心跳机制(MQTT协议)
  10. 6月14 空控制器和空操作及命名空间