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.
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.
$ 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.
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.
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.
$ pip install cprofilev
Once installed, you can use the cprofilev
command.
$ 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