Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the performance of Python Profiling

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

In this issue, the editor will bring you about how to analyze the performance of Python Profiling. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Performance analysis (Profiling) can be used to analyze where the running time of the program is mainly consumed in order to effectively optimize the running efficiency of the program.

Profiling can be divided into two steps: one is to run the program to collect data from different parts of the program, and the other is to visualize and analyze the data.

Tip:

The methods described below are mainly for Linux-like systems, and some of the tools may also be available in systems such as Windows.

CProfilePython Profiling data acquisition

Python's cProfile module can be used for data acquisition, suitable for Python 2 and Python 3. The calling method is simple:

The following call to import cProfile# runs the function somefunc () and records the relevant data to log_file.pyprofcProfile.run ('somefunc ()', 'log_file.pyprof')

Please refer to the Python Profiler documentation for more information.

Some Mini Program can also call the cProfile module directly from the command line to execute [1]:

Python-m cProfile-o profile_data.pyprof script_to_profile.pyhotshot

Hotshot is a high-performance Profiling data acquisition tool, its runtime has little impact on program efficiency, but it will produce huge running records and slow analysis. [2] there is no hotshot in Python 3. So if there are no special needs, please use cProfile.

Import hotshotprofiler = hotshot.Profile ("hotshot.log") profiler.run ('trackStereo.solveStereoNew ()') data visualization Gprof2Dot

The output of Gprof2Dot, the picture rendered by the dot command.

Gprof2Dot can convert a variety of Profiler data into image representations that can be processed by Graphviz. With the dot command, the time analysis diagram consumed by different functions can be obtained. Take the record of processing cProfile as an example [3]:

# run the program to record data: # python-m cProfile-o profile_data.pyprof path/to/your/script arg1 arg2 # profile_data.pyprof is the acquired data; Graphviz is required for the dot command to use gprof2dot.py-f pstats profile_data.pyprof | dot-Tpng-o output.pngRun Snake Run

Run Snake Run screenshot

RunSnakeRun is a Python script that uses wxPython to visualize Profiler data as shown in the figure.

RunSnakeRun can also analyze memory footprint, but it is still in the experimental stage. [4]

KCacheGrind

KCacheGrind Visualization Python Runtime data

KCacheGrind is a commonly used profiling visualization software in Linux, which can handle the output of valgrind by default. Some scripts can also allow it to analyze the data recorded by cProfile or hotshot.

Data for cProfile can be processed using pyprof2calltree:

# run the program to record data: # python-m cProfile-o profile_data.pyprof path/to/your/script arg1 arg2 # use pyprof2calltree to process data and automatically call KCacheGrindpyprof2calltree-I profile_data.pyprof-k

You can use the hotshot2calltree command in KCacheGrind to process the data for hotshot:

# use hotshot2calltree to process data, and manually open the output file hotshot2calltree hotshot.log-o hs_calltree.log in KCacheGrind after completion

Note:

Although KCacheGrind is powerful, the analysis tree of its output seems to be incomplete. If you know why, please add.

The above is the editor for you to share how to carry out Python Profiling performance analysis, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report