python性能分析工具模块_Python Profilers 分析器

 2023-09-07 阅读 19 评论 0

摘要:实时用户手册 本节是为 “不想阅读手册” 的用户提供的。它提供了非常简短的概述,并允许用户快速对现有应用程序执行评测。 要分析采用单个参数的函数,可以执行以下操作: import cProfile import re python运行效率。cProfile.run('re.compile("

实时用户手册¶

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

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

import cProfile

import re

python运行效率。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)

php7和python3性能对比,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)

python3.7、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指定的函数及其所有子函数(从调用到退出)消耗的累积时间。这个数字对于递归函数来说是准确的。

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

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

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

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

import cProfile

import re

python模块有哪些?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 的情况

python time模块?-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

python高性能编程,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

日常办公会用到的python模块?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

python re模块、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

python模块导入。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__')

python 内存分析工具。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

python和delphi哪个好,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.

python在日常办公中的应用,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.

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/12310.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息