日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2023/12/15 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python性能分析工具_Python Profilers 分析器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實時用戶手冊?

本節是為 “不想閱讀手冊” 的用戶提供的。它提供了非常簡短的概述,并允許用戶快速對現有應用程序執行評測。

要分析采用單個參數的函數,可以執行以下操作:

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 分析器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。