首页 > 其他 > 详细

高效编程之 cProfile 性能分析

时间:2019-09-26 18:15:19      阅读:67      评论:0      收藏:0      [点我收藏+]

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

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

高效编程之 cProfile 性能分析

原文:https://www.cnblogs.com/yanshw/p/11593255.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!