计算Python代码运行时间

计算Python代码运行时间

使用time包中的time.time()方法可以进行时间的过去。

查看API文档(3.6.5),给出的解释如下所示:

Return the time in seconds since the epoch as a floating point number. The specific date of the epoch and the handling of leap seconds is platform dependent. On Windows and most Unix systems, the epoch is January 1, 1970, 00:00:00 (UTC) and leap seconds are not counted towards the time in seconds since the epoch. This is commonly referred to as Unix time. To find out what the epoch is on a given platform, look at gmtime(0).

返回的就是从1970年1月1日到现在时间(秒)。

可以在程序的开始和结尾,分别调用这个函数,取差值就是大致程序运行的时间。

import time

start = time.time()

# func()

end = time.time()

print(end - start)

输出的就是程序大致运行的时间,单位为秒。

猜你喜欢

转载自blog.csdn.net/idealcitier/article/details/79910962