python时间测量

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

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.935462web

使用cProfileapp

在学习过程当中有什么不懂得能够加
个人python学习交流扣扣qun,688244617
群里有不错的学习教程、开发工具与电子书籍。
与你分享python企业当下人才需求及怎么从零基础学习好python,和学习什么内容。


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}

可以显示各类操做的耗时。svg

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