python:查看程序运行时间

需求

  • 查看程序运行时间;
  • 比较不同算法的执行效率

方法

time模块中的clock()

实例

import time

def test():
    start=time.clock()
    a=0
    for i in range(1000000):
        a+=i
    end=time.clock()
    total_time=end-start
    print("总耗时:"+str(total_time))

if __name__=="__main__":
    test()

输出结果为:
总耗时:0.0733818076715883

发布了47 篇原创文章 · 获赞 33 · 访问量 31万+

猜你喜欢

转载自blog.csdn.net/kaever/article/details/68945262