Tune/Speed up Python Code Performance Using cprofile code profiler and cprofilev viewer

Simple things become even simpler when they are explained properly and with hands on experience.
Python code and community is huge and there are several ways to profile your code.Lets us see a simple way using "cprofile"
Steps
1. Run the cprofile profiler to generate a .prof file
2. Install cprofilev to view the .prof file generated above
3. View the file on localhost browser and enjoy the performance tuning.

Step 1 : You can run the cprofile from the directory where you file is located or with the reference to path. e.g.
python -m cProfile -o outputprofile.prof yourfile.py
This will generate outputprofile.prof file in the directory where you ran it from

Step 2 : Install cprofilev as (use pip3 or pip based on the python version you are using)
pip install cprofilev

Step 3 Run the cprofilev with the following command
cprofilev -f outputprofile.prof
You will see : cProfile output available at http://127.0.0.1:4000
You will see the output as below, and you can see which function calls are taking long and try to resolve them.




Comments