计算程序运行时间

Posted panlj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计算程序运行时间相关的知识,希望对你有一定的参考价值。

time模块下的perf_counter方法可用于精确计算程序运行的时间:

 1 import time
 2 
 3 start = time.perf_counter()
 4 l = list(range(10000))
 5 end = time.perf_counter()
 6 run_time= end - start
 7 print(run_time)
 8 
 9 ‘‘‘
10 0.000290873000267311
11 ‘‘‘

 

perf_counter方法会把sleep的时间包含进去

 1 import time
 2 
 3 start = time.perf_counter()
 4 l = list(range(10000))
 5 time.sleep(1)
 6 end = time.perf_counter()
 7 run_time= end - start
 8 print(run_time)
 9 
10 ‘‘‘
11 1.000621135000074
12 ‘‘‘

 

以上是关于计算程序运行时间的主要内容,如果未能解决你的问题,请参考以下文章