Quickly profile Python scripts

Note that when I wanted to find out where the Python script was heavy and the bottleneck, I always forgot how to profile and re-examined it.

Profiling with cProfile

The profiler is available as standard in Python 3.

Python Profiler — Python 3 Documentation

There may be a higher performance profiler, but this time I will use it because the purpose is to profile it quickly.

When profiling the entire script

$ python -m cProfile -o profile.stats myscript.py

If -o <filename> is specified, the result will be output to a file instead of the standard output.

When profiling only part of a script

myscript.py


import cProfile
import very_slow_function

cProfile.run('very_slow_function()', 'profile.stats')

Wrap the process you want to profile with cProfile.run. If a file name is specified in the second argument, the result will be output to a file instead of being output to standard output.

View profiling results with cProfileV

The result file output by cProfile is binary data and cannot be viewed as it is. It can be read using the standard pstats module, but I can't remember how to use it, so this time I will browse using a tool called cProfileV.

Install cProfileV

$ pip install cprofilev

Once installed, you can use the cprofilev command.

Use cProfileV

$ cprofilev -f profile.stat

If you specify the result file with the -f option and execute the cprofilev command, the HTTP server will start, so you can view the profiling results by accessing http: // localhost: 4000 / from your browser.

It is convenient because you can sort by the number of executions and execution time of each method.

Recommended Posts

Quickly profile Python scripts
Dynamically import scripts in Python
Indentation formatting for python scripts
Get Python scripts to run quickly in Cloud Run using responder
Python
Utilize Python custom scripts with StackStorm
Run Python scripts synchronously from C #
Check installed modules from Python scripts
Run Python Scripts from Cisco Memorandum_EEM
How to package and distribute Python scripts
WebUI test with Python2.6 + Selenium 2.44.0 --profile setting
Quickly create an excel file with Python #python
Driver script for parametrically calculating Python scripts
Template for writing batch scripts in python
Quickly try Microsoft's Face API in Python
[Python] Quickly create an API with Flask