码迷,mamicode.com
首页 > 编程语言 > 详细

python时间测量

时间:2019-08-30 23:19:49      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:profiler   now()   显示   lis   结束   end   cal   开始   结束时间   

  • 使用自定义装饰器测量时间

def test_time(func):
    def inner(*args, **kw):
        t1 = datetime.datetime.now()
        print('开始时间:', t1)
        func(*args, **kw)
        t2 = datetime.datetime.now()
        print('结束时间:', t2)
        print('耗时: ', t2 - t1)

    return inner


@test_time
def call():
    a = list()
    for i in range(1000 * 10000):
        a .append(i)

输出结果:
开始时间: 2019-08-30 22:22:01.881215
结束时间: 2019-08-30 22:22:02.816677
耗时: 0:00:00.935462

  • 使用cProfile

def func():
    a2 = list()
    for i in range(100000):
        a2.append(i)


if __name__ == '__main__':

        al = list()
        for i in range(2000000):
            al.append(i)

        func()

ncalls tottime percall cumtime percall filename:lineno(function)
1 0.254 0.254 0.405 0.405 test.py:8()
1 0.009 0.009 0.013 0.013 test.py:8(func)
1 0.000 0.000 0.405 0.405 {built-in method builtins.exec}
2100000 0.142 0.000 0.142 0.000 {method ‘append‘ of ‘list‘ objects}
1 0.000 0.000 0.000 0.000 {method ‘disable‘ of ‘_lsprof.Profiler‘ objects}

能够显示各种操作的耗时。

  • 更直观的逐行代码时间测量line_profiler
    没装成功。

python时间测量

标签:profiler   now()   显示   lis   结束   end   cal   开始   结束时间   

原文地址:https://www.cnblogs.com/bryant24/p/11437436.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!