python计算程序运算时间的方法
小编给大家分享一下python计算程序运算时间的方法,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!
python计算程序运算时间的方法:
方法1:
importdatetimestarttime=datetime.datetime.now()#longrunning#dosomethingotherendtime=datetime.datetime.now()print(endtime-starttime).seconds
datetime.datetime.now()获取的是当前日期,在程序执行结束之后,这个方式获得的时间值为程序执行的时间。
方法2:
start=time.time()#longrunning#dosomethingotherend=time.time()printend-start
time.time()获取自纪元以来的当前时间(以秒为单位)。如果系统时钟提供它们,则可能存在秒的分数。所以这个地方返回的是一个浮点型类型。这里获取的也是程序的执行时间。
方法3:
start=time.clock()#longrunning#dosomethingotherend=time.clock()printend-start
time.clock()返回程序开始或第一次被调用clock()以来的CPU时间。 这具有与系统记录一样多的精度。返回的也是一个浮点类型。这里获得的是CPU的执行时间。
注:程序执行时间=cpu时间 + io时间 + 休眠或者等待时间
看完了这篇文章,相信你对python计算程序运算时间的方法有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。