Table of Contents
Use cProfile to quickly see the overall time-consuming
Use pstats to view detailed data and filter it
Use line_profiler to see the time overhead of each line
Use graphical tools to see the call relationship more intuitively
Home Backend Development Python Tutorial How to profile Python code?

How to profile Python code?

Jul 02, 2025 pm 03:07 PM

Python code performance analysis can be implemented through multiple tools. 1. Use cProfile to quickly view the overall time-consuming, run it through python -m cProfile your_script.py and find the most time-consuming function; 2. Use the pstats module to load the profiling result file, sort or filter the data as needed, and view only the key functions; 3. Use line_profiler to locate the time-consuming problem of specific lines of code, mark the objective function through the decorator and output the line-by-line execution time; 4. Use graphical tools such as SnakeViz or Py-Spy to intuitively display the call relationship and hotspot functions to assist in optimization decision-making.

How to profile Python code?

If you want to know whether Python code runs fast or where it is stuck, the most direct way is to perform performance analysis. It is difficult to say this, but there are indeed many details to pay attention to. Python provides several useful tools that can help you find the bottleneck.

How to profile Python code?

Use cProfile to quickly see the overall time-consuming

This is a performance analysis module that comes with the standard library, which is suitable for running the program quickly to see which functions take the most time. It is also very simple to use:

How to profile Python code?
 python -m cProfile your_script.py

The output results will list the number of calls per function, the total run time, the average time per time, and other information. If you just want to know "which function slows down the entire process", this is enough.

Tips: Adding -s tottime can be sorted by total execution time, making it easier to see the problem points.

How to profile Python code?

Use pstats to view detailed data and filter it

cProfile output too much? You can save the result of profiling into a file, and then use the pstats module to interactively view or write scripts to filter key data:

 python -m cProfile -o output.prof your_script.py

Then load and view through the following code:

 import pstats
p = pstats.Stats('output.prof')
p.sort_stats(stats.SortKey.TIME).print_stats(10)

This way you can only look at the top 10 most time-consuming functions. You can also filter by file name and function name, which is very flexible.


Use line_profiler to see the time overhead of each line

The above two tools can only tell you how long it took for a certain function to take in total, but don't know which line is dragging you down. At this time, line_profiler is needed, which can accurately reach the execution time of each line of code.

Installation method:

 pip install line_profiler

Add a decorator when using:

 from line_profiler import LineProfiler

def my_slow_function():
    ...

lp = LineProfiler()
lp_wrapper = lp(my_slow_function)
lp_wrapper()
lp.print_stats()

After running, you can see the number of calls and time-consuming calls of each line, which is especially suitable for troubleshooting performance traps in loops or algorithms.


Use graphical tools to see the call relationship more intuitively

Text output is sometimes not intuitive enough, especially when functions are nested and called a lot. Profiling data can be visualized using tools like SnakeViz or Py-Spy .

For example, SnakeViz supports generating web charts from .prof files, and you can see who is the "hot spot" function at a glance. Py-Spy can view the process stack in real time without modifying the code, suitable for online services or long-running programs.


These methods can basically locate most performance problems by using them together. The key is not to use it all at once, but to choose the right tool according to the specific situation. For example, when you start, use cProfile to view the overall situation, and then use line_profiler to accurately analyze a certain piece of code after determining the scope.

Basically, that's all. Don't underestimate them. Many codes that seem slow actually just add a cache or change a data structure.

The above is the detailed content of How to profile Python code?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1509
276
Can a Python class have multiple constructors? Can a Python class have multiple constructors? Jul 15, 2025 am 02:54 AM

Yes,aPythonclasscanhavemultipleconstructorsthroughalternativetechniques.1.Usedefaultargumentsinthe__init__methodtoallowflexibleinitializationwithvaryingnumbersofparameters.2.Defineclassmethodsasalternativeconstructorsforclearerandscalableobjectcreati

Python for Quantum Machine Learning Python for Quantum Machine Learning Jul 21, 2025 am 02:48 AM

To get started with quantum machine learning (QML), the preferred tool is Python, and libraries such as PennyLane, Qiskit, TensorFlowQuantum or PyTorchQuantum need to be installed; then familiarize yourself with the process by running examples, such as using PennyLane to build a quantum neural network; then implement the model according to the steps of data set preparation, data encoding, building parametric quantum circuits, classic optimizer training, etc.; in actual combat, you should avoid pursuing complex models from the beginning, paying attention to hardware limitations, adopting hybrid model structures, and continuously referring to the latest documents and official documents to follow up on development.

Accessing data from a web API in Python Accessing data from a web API in Python Jul 16, 2025 am 04:52 AM

The key to using Python to call WebAPI to obtain data is to master the basic processes and common tools. 1. Using requests to initiate HTTP requests is the most direct way. Use the get method to obtain the response and use json() to parse the data; 2. For APIs that need authentication, you can add tokens or keys through headers; 3. You need to check the response status code, it is recommended to use response.raise_for_status() to automatically handle exceptions; 4. Facing the paging interface, you can request different pages in turn and add delays to avoid frequency limitations; 5. When processing the returned JSON data, you need to extract information according to the structure, and complex data can be converted to Data

python one line if else python one line if else Jul 15, 2025 am 01:38 AM

Python's onelineifelse is a ternary operator, written as xifconditionelsey, which is used to simplify simple conditional judgment. It can be used for variable assignment, such as status="adult"ifage>=18else"minor"; it can also be used to directly return results in functions, such as defget_status(age):return"adult"ifage>=18else"minor"; although nested use is supported, such as result="A"i

Completed python blockbuster online viewing entrance python free finished website collection Completed python blockbuster online viewing entrance python free finished website collection Jul 23, 2025 pm 12:36 PM

This article has selected several top Python "finished" project websites and high-level "blockbuster" learning resource portals for you. Whether you are looking for development inspiration, observing and learning master-level source code, or systematically improving your practical capabilities, these platforms are not to be missed and can help you grow into a Python master quickly.

python if else example python if else example Jul 15, 2025 am 02:55 AM

The key to writing Python's ifelse statements is to understand the logical structure and details. 1. The infrastructure is to execute a piece of code if conditions are established, otherwise the else part is executed, else is optional; 2. Multi-condition judgment is implemented with elif, and it is executed sequentially and stopped once it is met; 3. Nested if is used for further subdivision judgment, it is recommended not to exceed two layers; 4. A ternary expression can be used to replace simple ifelse in a simple scenario. Only by paying attention to indentation, conditional order and logical integrity can we write clear and stable judgment codes.

python seaborn jointplot example python seaborn jointplot example Jul 26, 2025 am 08:11 AM

Use Seaborn's jointplot to quickly visualize the relationship and distribution between two variables; 2. The basic scatter plot is implemented by sns.jointplot(data=tips,x="total_bill",y="tip",kind="scatter"), the center is a scatter plot, and the histogram is displayed on the upper and lower and right sides; 3. Add regression lines and density information to a kind="reg", and combine marginal_kws to set the edge plot style; 4. When the data volume is large, it is recommended to use "hex"

python run shell command example python run shell command example Jul 26, 2025 am 07:50 AM

Use subprocess.run() to safely execute shell commands and capture output. It is recommended to pass parameters in lists to avoid injection risks; 2. When shell characteristics are required, you can set shell=True, but beware of command injection; 3. Use subprocess.Popen to realize real-time output processing; 4. Set check=True to throw exceptions when the command fails; 5. You can directly call chains to obtain output in a simple scenario; you should give priority to subprocess.run() in daily life to avoid using os.system() or deprecated modules. The above methods override the core usage of executing shell commands in Python.

See all articles