zoukankan      html  css  js  c++  java
  • 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.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
      没装成功。
  • 相关阅读:
    java8中常用的新功能
    代码走查案例2
    redis的key设置每天凌晨过期的思路
    代码走查案例1
    一场没有序列化引发的血案
    面试总结
    hr相关的
    mysql底层原理
    如何学好并发编程
    流程图总结
  • 原文地址:https://www.cnblogs.com/bryant24/p/11437436.html
Copyright © 2011-2022 走看看