Python 代码运行时间的3种计算方法

来源:互联网 发布:楼月软件 编辑:程序博客网 时间:2024/06/06 01:59

方法1

?
1
2
3
4
5
importdatetime
starttime=datetime.datetime.now()
#long running
endtime=datetime.datetime.now()
print(endtime -starttime).seconds

方法 2

?
1
2
3
4
start=time.time()
run_fun()
end=time.time()
printend-start

方法3

?
1
2
3
4
start=time.clock()
run_fun()
end=time.clock()
printend-start

方法1和方法2都包含了其他程序使用CPU的时间,是程序开始到程序结束的运行时间。 

方法3算只计算了程序运行的CPU时间