本文摘自php中文网,作者anonymity,侵删。
python脚本使用统计时间的方式是time.clock()
接下来,就几种python的统计时间方式对比一下:
方法1:
1 2 3 4 5 6 7 | import datetime
starttime = datetime.datetime.now()
#long running
# do something other
endtime = datetime.datetime.now()
print (endtime - starttime).seconds
datetime.datetime.now()获取的是当前日期,在程序执行结束之后,这个方式获得的时间值为程序执行的时间。
|
方法2:
1 2 3 4 5 | start = time.time()
#long running
# do something other
end = time.time()
print end -start
|
time.time()获取自纪元以来的当前时间(以秒为单位)。如果系统时钟提供它们,则可能存在秒的分数。所以这个地方返回的是一个浮点型类型。这里获取的也是程序的执行时间。
方法3:
1 2 3 4 5 | start = time.clock()
#long running
# do something other
end = time.clock()
print end -start
|
time.clock()返回程序开始或第一次被调用clock()以来的CPU时间。 这具有与系统记录一样多的精度。返回的也是一个浮点类型。这里获得的是CPU的执行时间。
注:程序执行时间=cpu时间 + io时间 + 休眠或者等待时间
以上就是Python如何测量脚本运行时间的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python中matplotlib模块用例(代码)
vs code Python怎么使用的?
Python做app用什么工具
Python 读取图片文件为矩阵和保存矩阵为图片的方法
怎么用Python绘制圆
Python属于什么类型语言
Python怎么输入一个三位数,输出百位、十位和个位
Python中子类如何调用父类函数的代码示例
Python end用法是什么?
Python命令行参数是什么
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » Python如何测量脚本运行时间