One - One Code All

Blog Content

python计算程序运行时间

Python   2009-10-30 22:34:17

方法1

import datetime

starttime = datetime.datetime.now()
#long running
endtime = datetime.datetime.now()
print (endtime - starttime).seconds

方法 2

start = time.time()
run_fun()
end = time.time()
print end-start

方法3

start = time.clock()
run_fun()
end = time.clock()
print end-start

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

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


上一篇:Python中的__init__和__new__
下一篇:python中list和dict作为全局变量无需global声明

The minute you think of giving up, think of the reason why you held on so long.