python运行时间的几种方法
2016-06-17 00:00
706 查看
最早见过手写的,类似于下面这种:
输出如下:
➜ Python python time_1.py
0:00:00.280797
另外一种方法是使用timeit模块,使用方法如下:
还可以在命令行上使用这种timeit模块,如下:
注意:timeit模块会多次运行程序以获得更精确的时间,所以需要避免重复执行带来的影响。比方说x.sort()这种操作,因为第一次执行之后,后边已经是排好的了,准确性就收到了影响。
还有一种方法是使用cProfile模块,代码如下,名字为time_1.py:
运行程序结果如下:
一开始代码里最后一行写的是cProfile.run('main()'),提示没有main(),将main()改成函数名字就可以了。
以上就是本文的全部内容,希望对大家学习python程序设计有所帮助。
python中关于时间和日期函数的常用计算总结(time和datatime)
python计算程序开始到程序结束的运行时间和程序运行的CPU时间
Python时间戳与时间字符串互相转换实例代码
Python获取当前时间的方法
python操作日期和时间的方法
Python计算程序运行时间的方法
10种检测Python程序运行时间、CPU和内存占用的方法
Python中实现对Timestamp和Datetime及UTC时间之间的转换
python正常时间和unix时间戳相互转换的方法
import datetime def time_1(): begin = datetime.datetime.now() sum = 0 for i in xrange(10000000): sum = sum + i end = datetime.datetime.now() return end-begin 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:
import datetime def time_1(): begin = datetime.datetime.now() sum = 0 for i in xrange(10000000): sum = sum + i end = datetime.datetime.now() return end-begin if __name__ == '__main__': print time_1() import cProfile 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 <string>:1(<module>) 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 <module> 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 "<string>", line 1, in <module> 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 <string>:1(<module>) 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()改成函数名字就可以了。
以上就是本文的全部内容,希望对大家学习python程序设计有所帮助。
您可能感兴趣的文章:
Python 时间处理datetime实例python中关于时间和日期函数的常用计算总结(time和datatime)
python计算程序开始到程序结束的运行时间和程序运行的CPU时间
Python时间戳与时间字符串互相转换实例代码
Python获取当前时间的方法
python操作日期和时间的方法
Python计算程序运行时间的方法
10种检测Python程序运行时间、CPU和内存占用的方法
Python中实现对Timestamp和Datetime及UTC时间之间的转换
python正常时间和unix时间戳相互转换的方法
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- 如何在 Linux 上安装、配置 NTP 服务器和客户端?