zoukankan      html  css  js  c++  java
  • 高效编程之 cProfile 性能分析

    写代码经常会听说一些名词,比如 性能分析、代码调优。

    cProfile 是 python 代码调优的一种工具,它能够统计在整个代码执行过程中,每个函数调用的次数和消耗的时间。

    这个工具虽然很常用,但是没必要花太多时间研究这个工具,简单使用就能达到效果,所以我这里只简单记录下核心用法。

    两种使用方式

    cProfile.run('func(arg)')     # 调优函数,在脚本中使用
    python -m cProfile myscript.py (-s ziduan)    # 调优脚本,在命令行使用

    输出解释

            9891015 function calls (9843181 primitive calls) in 12.791 seconds
    
       Ordered by: standard name
    
       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
            1    0.000    0.000   12.791   12.791 <string>:1(<module>)
            1    0.037    0.037    0.966    0.966 AccOverstep.py:13(AccOverstep)
            2    0.166    0.083    9.534    4.767 DataPreprocessing.py:27(dists)
            1    0.003    0.003    6.252    6.252 DataPreprocessing.py:38(DataPreprocessing)
        10909    0.004    0.000    0.167    0.000 _methods.py:34(_sum)
           10    0.000    0.000    0.000    0.000 _methods.py:45(_all)
        42360    0.071    0.000    0.092    0.000 _methods.py:48(_count_reduce_items)
        42360    0.201    0.000    0.678    0.000 _methods.py:58(_mean)

    共有 9891015 次函数调用,原始调用为 9843181 次,原始调用代表不包含递归调用。

    ncalls 函数的被调用次数 

    tottime 函数总计运行时间,除去函数中调用的函数运行时间 

    percall 函数运行一次的平均时间,等于tottime/ncalls 

    cumtime 函数总计运行时间,含调用的函数运行时间 

    percall 函数运行一次的平均时间,等于cumtime/ncalls 

    filename:lineno(function) 函数所在的文件名,函数的行号,函数名
     

    参考资料:

    https://blog.csdn.net/u010453363/article/details/78415553

  • 相关阅读:
    Linux、CentOS系统下调整home和根分区大小
    Dell服务器Raid卡电池策略调整
    自建Saltstack的repo软件源仓库
    CentOS6.6下DRBD+HeartBeat+NFS配置
    go vendor目录
    cmd/go: unsupported GOOS/GOARCH pair linux /amd64
    linux的单用户模式
    go import导入的理解
    监控系统整合告警平台实现邮件、短信、微信、电话语音和app等多方式报警
    如何以非交互方式为psql指定密码?
  • 原文地址:https://www.cnblogs.com/yanshw/p/11593255.html
Copyright © 2011-2022 走看看