cProfile是标准库内建的分析工具的其中一个,另外两个是hotshot和profile

-s cumulative

-s cumulative开关告诉cProfile对每个函数累计花费的时间进行排序,他能让我看到代码最慢的部分。

我们有这样一个函数。

loopdemo.py

def foo():
for a in range(0, 101):
for b in range(0, 101):
if a + b == 100:
yield a, b
if __name__ == '__main__':
for item in foo():
print(item)

运行下面命令

python3 -m cProfile -s cumulative loopdemo.py

得到如下结果

         206 function calls in 0.001 seconds
#在0.01秒内共发生了206次函数调用。包括cProfile的开销。 Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.001 0.001 {built-in method builtins.exec}
1 0.000 0.000 0.001 0.001 loopdemo.py:7(<module>)
102 0.001 0.000 0.001 0.000 loopdemo.py:7(foo)
101 0.001 0.000 0.001 0.000 {built-in method builtins.print}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}

其中对参数的解释:

ncalls:表示函数调用的次数;

tottime:表示指定函数的总的运行时间,除掉函数中调用子函数的运行时间;

percall:(第一个percall)等于 tottime/ncalls;

cumtime:表示该函数及其所有子函数的调用运行的时间,即函数开始调用到返回的时间;

percall:(第二个percall)即函数运行一次的平均时间,等于 cumtime/ncalls;

filename:lineno(function):每个函数调用的具体信息;

需要注意的是cProfile很难搞清楚函数内的每一行发生了什么,是针对整个函数来说的。

-o profile.stats

我们可与你通过这个函数将结果输出到一个文件中,当然文件的后缀名是任意的,这里为了方便后面配合python中使用所以将后缀定为stats。

首先让我们运行下面的命令

python3 -m cProfile -o loopdemo_profile.stats loopdemo.py

然后运行下面的脚本

import pstats
p=pstats.Stats("loopdemo_profile.stats")
p.sort_stats("cumulative")
p.print_stats()
p.print_callers() # 可以显示函数被哪些函数调用
p.print_callees() # 可以显示哪个函数调用了哪些函数

可以看到输出了和之前控制台一样的结果


2006 function calls in 0.005 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.005 0.005 {built-in method builtins.exec}
1 0.000 0.000 0.005 0.005 loopdemo.py:7(<module>)
1001 0.004 0.000 0.004 0.000 {built-in method builtins.print}
1002 0.000 0.000 0.000 0.000 loopdemo.py:30(foo2)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} Ordered by: cumulative time Function was called by...
ncalls tottime cumtime
{built-in method builtins.exec} <-
loopdemo.py:7(<module>) <- 1 0.000 0.005 {built-in method builtins.exec}
{built-in method builtins.print} <- 1001 0.004 0.004 loopdemo.py:7(<module>)
loopdemo.py:30(foo2) <- 1002 0.000 0.000 loopdemo.py:7(<module>)
{method 'disable' of '_lsprof.Profiler' objects} <- Ordered by: cumulative time Function called...
ncalls tottime cumtime
{built-in method builtins.exec} -> 1 0.000 0.005 loopdemo.py:7(<module>)
loopdemo.py:7(<module>) -> 1002 0.000 0.000 loopdemo.py:30(foo2)
1001 0.004 0.004 {built-in method builtins.print}
{built-in method builtins.print} ->
loopdemo.py:30(foo2) ->
{method 'disable' of '_lsprof.Profiler' objects} ->

line_profiler

安装

pip3 install Cpython
pip3 install Cython git+https://github.com/rkern/line_profiler.git

python性能分析之cProfile模块的更多相关文章

  1. python性能分析之line_profiler模块

    line_profiler使用装饰器(@profile)标记需要调试的函数.用kernprof.py脚本运行代码,被选函数每一行花费的cpu时间以及其他信息就会被记录下来. 安装 pip3 insta ...

  2. Python性能分析

    Python性能分析 https://www.cnblogs.com/lrysjtu/p/5651816.html https://www.cnblogs.com/cbscan/articles/33 ...

  3. 如何进行 Python性能分析,你才能如鱼得水?

    [编者按]本文作者为 Bryan Helmig,主要介绍 Python 应用性能分析的三种进阶方案.文章系国内 ITOM 管理平台 OneAPM 编译呈现. 我们应该忽略一些微小的效率提升,几乎在 9 ...

  4. Python性能分析工具Profile

    Python性能分析工具Profile 代码优化的前提是需要了解性能瓶颈在什么地方,程序运行的主要时间是消耗在哪里,对于比较复杂的代码可以借助一些工具来定位,python 内置了丰富的性能分析工具,如 ...

  5. python性能分析(一)——使用timeit给你的程序打个表吧

    前言 我们可以通过查看程序核心算法的代码,得知核心算法的渐进上界或者下界,从而大概估计出程序在运行时的效率,但是这并不够直观,也不一定十分靠谱(在整体程序中仍有一些不可忽略的运行细节在估计时被忽略了) ...

  6. Python性能分析与优化PDF高清完整版免费下载|百度云盘

    百度云盘|Python性能分析与优化PDF高清完整版免费下载 提取码:ubjt 内容简介 全面掌握Python代码性能分析和优化方法,消除性能瓶颈,迅速改善程序性能! 对于Python程序员来说,仅仅 ...

  7. python性能分析--cProfile

    Python标准库中提供了三种用来分析程序性能的模块,分别是cProfile, profile和hotshot,另外还有一个辅助模块stats.这些模块提供了对Python程序的确定性分析功能,同时也 ...

  8. Python丨Python 性能分析大全

    虽然运行速度慢是 Python 与生俱来的特点,大多数时候我们用 Python 就意味着放弃对性能的追求.但是,就算是用纯 Python 完成同一个任务,老手写出来的代码可能会比菜鸟写的代码块几倍,甚 ...

  9. Python 性能分析工具简介

    Table of Contents 1. 性能分析和调优工具简介 1.1. Context Manager 1.2. Decorator 1.3. 系统自带的time命令 1.4. python ti ...

随机推荐

  1. centos的用户、组权限、添加删除用户等操作的详细操作命令

    1.Linux操作系统是多用户多任务操作系统,包括用户账户和组账户两种 细分用户账户(普通用户账户,超级用户账户)除了用户账户以为还 有组账户所谓组账户就是用户账户的集合,centos组中有两种类型, ...

  2. LInux Zebra

    本文章摘自linux公社 https://www.linuxidc.com/Linux/2015-07/120224p1.htm   写在前面 从22号中午开始琢磨zebra/quagga的用法,一直 ...

  3. [Android] Android ViewPager 中加载 Fragment的两种方式 方式(二)

    接上文: https://www.cnblogs.com/wukong1688/p/10693338.html Android ViewPager 中加载 Fragmenet的两种方式 方式(一) 二 ...

  4. 细说tomcat之应用监控

    官网:http://tomcat.apache.org/tomcat-7.0-doc/monitoring.html Java应用程序的监控通过JMX实现,详见:https://docs.oracle ...

  5. electron入门

    Electron是由Github开发,用HTML,CSS和JavaScript来构建跨平台桌面应用程序的一个开源库. Electron通过将Chromium和Node.js合并到同一个运行时环境中,并 ...

  6. ASP.NET MVC 3 Razor 语法

    1.   三元运算符 1)   输出文本 1.   View var var1 = '@(1 < 2 ? "YES" : "NO")'; var var2 ...

  7. Git学习之连接GitHub远程仓库

    在看此教程之前电脑上应该已安装好git,并且配置好基本信息,Git新手请从头开始. 第1步:创建SSH Key 在用户主目录下(Mac系统是在用户主目录下,可通过命令ll -a查看,Windows下自 ...

  8. Coursera, Deep Learning 1, Neural Networks and Deep Learning - week2, Neural Networks Basics

    Logistic regression Cost function for logistic regression Gradient Descent 接下来主要讲 Vectorization Logi ...

  9. 2017CCPC秦皇岛 H题Prime Set&&ZOJ3988

    题意: 定义一种集合,只有两个数,两个数不同且加起来为素数.要从n个数里抽出数字组成该集合(数字也可以是1~n,这个好懵圈啊),要求你选择最多k个该种集合组成一个有最多元素的集合,求出元素的数量. 思 ...

  10. 进程池爬取并存入mongodb

    设置进程池爬取拉钩网: # coding = utf- import json import pymongo import pandas as pd import requests from lxml ...