When you will have efficiency issues in your Python utility, you should use a Profiler that will help you.
Step 1: Utilizing cProfile
Your first possibility is to run your utility with
-m cProfile within the cli.
Let’s take an instance utility that’s run as follows:
We will run it with a Profiler by doing the next:
python -m cProfile -o outfile app.py
This provides you with a report that reveals the place more often than not is spent whereas working your app.
Step 2: Utilizing LineProfiler (advisable)
My most popular possibility, is to make use of the
line_profiler instrument to carry out a extra detailed scan.
Begin by putting in the
pip set up line_profiler
Then we will modify our utility code and add the
@profile annotation proper earlier than every operate you’d wish to see statistics with.
@profile def fun_a(): #do one thing @profile def fun_b(): #do one thing extra if __name__ == '__main__': fun_a() fun_b()
Now we will run the profiler utilizing the beneath command:
time kernprof -l -v app.py