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. Django之Models操作

    一.字段 AutoField(Field) - int自增列,必须填入参数 primary_key=True BigAutoField(AutoField) - bigint自增列,必须填入参数 pr ...

  2. Nginx自定义404页面并返回404状态码

    Nginx定义404页面并返回404状态码, WebServer是nginx,直接告诉我应该他们配置了nginx的404错误页面,虽然请求不存在的资源可以成功返回404页面,但返回状态码确是200. ...

  3. mavan安装本地jar

    mvn install:install-file -Dfile=E:\IDEAworkspace\importAnalysisDemo\lib\IKAnalyzer2012_u6.jar -Dgrou ...

  4. linux 精简开机自启动

    centos7 精简开机自启动 ntsysv rsyslog  crond  sshd network

  5. spring cloud学习填坑笔记

    最近在学习spring cloud,由于学习资料具有普遍性,部分应个人原因导致的小细节问题,往往很难找到解决的办法.这特别记录一下自己遇到的一些问题. 一.eureka-server加入securit ...

  6. SpringBoot系列: Redis 共享Session

    Web项目Session管理是一个很重要的话题, 涉及到系统横向扩展, SpringBoot已经为共享Session很好的解决方案, 这篇文章关注使用Redis共享会话, 同时这也是最常用的方法. = ...

  7. 如何使用Cygwin在Windows上运行OpenSSH SSHD服务器

    记录几款非常有趣, 但不怎么耳熟的软件: Cygwin 是可以安装 OpenSSH server 和 client 的, Mosh 也可以, 这对于 Linux 用户而言就非常方便了. 如何使用Cyg ...

  8. Excel列名序号互转

    public static int ToIndex(string columnName) { if (!Regex.IsMatch(columnName.ToUpper(), @"[A-Z] ...

  9. C++ 出现异常“.... \debug_heap.cpp Line:980 Expression:__acrt_first_block==header"

    本人是在写dll项目中出现了这个问题,经过一天的研究,尝试了三个步骤1.在配置属性->常规->MFC的使用中,将在静态库中使用MFC改为在共享DLL中使用MFC.但是还会出错2.原因是dl ...

  10. ssh免密登录及去掉提示

    A连B 1.生成公钥和私钥    ssh-keygen2.复制公钥到需要免密登录的服务器上  ssh-copy-id root@192.168.25.175 A连A 2.复制公钥到本机上  ssh-c ...