实时用户手册¶

本节是为 “不想阅读手册” 的用户提供的。它提供了非常简短的概述,并允许用户快速对现有应用程序执行评测。

要分析采用单个参数的函数,可以执行以下操作:

import cProfile

import re

cProfile.run('re.compile("foo|bar")')

(如果 cProfile 在您的系统上不可用,请使用 profile 。)

上述操作将运行 re.compile() 并打印分析结果,如下所示:

197 function calls (192 primitive calls) in 0.002 seconds

Ordered by: standard name

ncalls tottime percall cumtime percall filename:lineno(function)

1 0.000 0.000 0.001 0.001 :1()

1 0.000 0.000 0.001 0.001 re.py:212(compile)

1 0.000 0.000 0.001 0.001 re.py:268(_compile)

1 0.000 0.000 0.000 0.000 sre_compile.py:172(_compile_charset)

1 0.000 0.000 0.000 0.000 sre_compile.py:201(_optimize_charset)

4 0.000 0.000 0.000 0.000 sre_compile.py:25(_identityfunction)

3/1 0.000 0.000 0.000 0.000 sre_compile.py:33(_compile)

第一行显示监听了197个调用。在这些调用中,有192个是 原始的 ,这意味着调用不是通过递归引发的。下一行: Ordered by: standard name ,表示最右边列中的文本字符串用于对输出进行排序。列标题包括:

ncalls调用次数

tottime在指定函数中消耗的总时间(不包括调用子函数的时间)

percall是 tottime 除以 ncalls 的商

cumtime指定的函数及其所有子函数(从调用到退出)消耗的累积时间。这个数字对于递归函数来说是准确的。

percall是 cumtime 除以原始调用(次数)的商(即:函数运行一次的平均时间)

filename:lineno(function)提供相应数据的每个函数

如果第一列中有两个数字(例如3/1),则表示函数递归。第二个值是原始调用次数,第一个是调用的总次数。请注意,当函数不递归时,这两个值是相同的,并且只打印单个数字。

profile 运行结束时,打印输出不是必须的。也可以通过为 run() 函数指定文件名,将结果保存到文件中:

import cProfile

import re

cProfile.run('re.compile("foo|bar")', 'restats')

pstats.Stats 类从文件中读取 profile 结果,并以各种方式对其进行格式化。

cProfile 和 profile 文件也可以作为脚本调用,以分析另一个脚本。例如:

python -m cProfile [-o output_file] [-s sort_order] (-m module | myscript.py)

-o 将profile 结果写入文件而不是标准输出

-s 指定 sort_stats() 排序值之一以对输出进行排序。这仅适用于未提供 -o 的情况

-m 指定要分析的是模块而不是脚本。

3.7 新版功能:cProfile 添加 -m 选项

3.8 新版功能:profile 添加 -m 选项

The pstats module's Stats class has a variety of methods

for manipulating and printing the data saved into a profile results file:

import pstats

from pstats import SortKey

p = pstats.Stats('restats')

p.strip_dirs().sort_stats(-1).print_stats()

The strip_dirs() method removed the extraneous path from all

the module names. The sort_stats() method sorted all the

entries according to the standard module/line/name string that is printed. The

print_stats() method printed out all the statistics. You

might try the following sort calls:

p.sort_stats(SortKey.NAME)

p.print_stats()

The first call will actually sort the list by function name, and the second call

will print out the statistics. The following are some interesting calls to

experiment with:

p.sort_stats(SortKey.CUMULATIVE).print_stats(10)

This sorts the profile by cumulative time in a function, and then only prints

the ten most significant lines. If you want to understand what algorithms are

taking time, the above line is what you would use.

If you were looking to see what functions were looping a lot, and taking a lot

of time, you would do:

p.sort_stats(SortKey.TIME).print_stats(10)

to sort according to time spent within each function, and then print the

statistics for the top ten functions.

你也可以尝试:

p.sort_stats(SortKey.FILENAME).print_stats('__init__')

This will sort all the statistics by file name, and then print out statistics

for only the class init methods (since they are spelled with __init__ in

them). As one final example, you could try:

p.sort_stats(SortKey.TIME, SortKey.CUMULATIVE).print_stats(.5, 'init')

This line sorts statistics with a primary key of time, and a secondary key of

cumulative time, and then prints out some of the statistics. To be specific, the

list is first culled down to 50% (re: .5) of its original size, then only

lines containing init are maintained, and that sub-sub-list is printed.

If you wondered what functions called the above functions, you could now (p

is still sorted according to the last criteria) do:

p.print_callers(.5, 'init')

and you would get a list of callers for each of the listed functions.

If you want more functionality, you're going to have to read the manual, or

guess what the following functions do:

p.print_callees()

p.add('restats')

Invoked as a script, the pstats module is a statistics browser for

reading and examining profile dumps. It has a simple line-oriented interface

(implemented using cmd) and interactive help.

python性能分析工具模块_Python Profilers 分析器相关推荐

  1. python性能分析工具模块_python——关于Python Profilers性能分析器

    1. 介绍性能分析器 profiler是一个程序,用来描述运行时的程序性能,并且从不同方面提供统计数据加以表述.Python中含有3个模块提供这样的功能,分别是cProfile, profile和ps ...

  2. python性能分析工具总结

    性能分析工具的使用 cProfile 介绍 它是一种确定性的性能分析器,提供了一组API帮助开 发者收集Python程序运行的信息,更确切地说,是统计每个函数消耗的 CPU时间.同时它还提供了其他细节 ...

  3. python性能分析工具

    1)cProfile cProfile可以嵌入到python代码中执行,比如: import cProfile cProfile.run('foo()', 'foo.out') 查看结果需要pstat ...

  4. python性能分析工具_Python Profilers 分析器

    实时用户手册¶ 本节是为 "不想阅读手册" 的用户提供的.它提供了非常简短的概述,并允许用户快速对现有应用程序执行评测. 要分析采用单个参数的函数,可以执行以下操作: import ...

  5. cProfile——Python性能分析工具

    Python自带了几个性能分析的模块:profile.cProfile和hotshot,使用方法基本都差不多,无非模块是纯Python还是用C写的.本文介绍cProfile.  例子 import t ...

  6. python输出程序运行时间_叨叨 Python 性能优化工具

    虽然Python是一个"慢慢的"语言,但是不代表我们对性能没有任何的追求,在程序运行过程中,如果发现程序运行时间太长或者内存占用过大,免不了需要对程序的执行过程进行一些监测,找到有 ...

  7. 前端性能分析工具利器

    作者:basinwang,腾讯 PCG 前端开发工程师 大型项目容易遇到性能问题,一般来说,当我们遇到性能瓶颈的时候,才会开始去进行相应的分析.分析的方向除了业务本身的特点相关之外,常见的还可以借助一 ...

  8. FISCO BCOS工程师常用的性能分析工具推荐

    FISCO BCOS是完全开源的联盟区块链底层技术平台,由金融区块链合作联盟(深圳)(简称金链盟)成立开源工作组通力打造.开源工作组成员包括博彦科技.华为.深证通.神州数码.四方精创.腾讯.微众银行. ...

  9. 系统级性能分析工具perf的介绍与使用

    测试环境:Ubuntu16.04 + Kernel:4.4.0-31 apt-get install linux-source cd /usr/src/tools/perf make &&am ...

最新文章

  1. NSwagStudio for Swagger Api
  2. project02 U盘系统与排错系统
  3. selenium在页面中多个fream的定位
  4. MySQL 5.7最新版本的2个bug
  5. php星座判断源码,php根据日期判断星座的函数分享
  6. plsql 中的记录型变量和引用型变量
  7. ubuntu16安装mysql8.0
  8. javascript瀑布流效果
  9. php环境编译成品,PHPweb成品网站安装环境要求和安装方法
  10. HTML之表格的基本知识
  11. hive和mysql传输数据类型_hive的数据类型
  12. P1117 [NOI2016]优秀的拆分
  13. BOA软件服务的移植和BOA服务的配置
  14. delphi pid判断进程结束_有两个这样的进程:僵尸进程amp;孤儿进程,蓝瘦香菇
  15. linux 查看设备 usb设备驱动程序,Linux USB设备驱动程序未被探测
  16. 如何设计一款IRR计算器
  17. 怎么才能优雅地向博士导师表达「这周科研没什么进展」?
  18. Greenplum小把戏 - 判断数值是否在区间内
  19. 学生php实训个人总结300字,实训总结300字通用版5篇
  20. slot具名卡槽和props

热门文章

  1. 【转】Linux的五个查找命令:find,locate,whereis,which,type
  2. 在web3上搭建ecshop网上商城
  3. playframework文档未提及,但你能做的事
  4. 使用Dundas控件在web应用上展现多维数据集(二)
  5. 面试官系统精讲Java源码及大厂真题 - 42 常用的 Lambda 表达式使用场景解析和应用
  6. GitHub 标星 1.6w+,我发现了一个宝藏项目,推荐大家学习
  7. 容器编排技术 -- Kubernetes kubectl edit 命令详解
  8. 在Centos7 更改Docker默认镜像和容器的位置
  9. .net core3.1 下由Autofac接管IOC
  10. 【Pyhton】随机漫步散点图