I'm running the following code line on my terminal to get a profile of my program.
python3 -m cProfile -s time main.py
However the output it prints is gigantic. I only want to know the 10 most time-consuming tasks or to sort them in ascending order. How can I tell this to cprof?
The syntax is cProfile. run(statement, filename=None, sort=-1) . You can pass python code or a function name that you want to profile as a string to the statement argument. If you want to save the output in a file, it can be passed to the filename argument.
Analysis of the profiler data is done using the Stats class. This class constructor creates an instance of a “statistics object” from a filename (or list of filenames) or from a Profile instance. Output will be printed to the stream specified by stream.
In Python's standard library, we have the timeit module that allows us to do some simple profiling. The output of timeit is to find the best performance among multiple runs (default to be 5).
I found the solution at this other answer. The solution is to use cProfile internally, within the script to be profiled, rather than externally at the command line.
My script looks like this:
def run_code_to_be_profiled():
pass
if __name__ == "__main__":
import cProfile
pr = cProfile.Profile()
pr.enable()
run_code_to_be_profiled()
pr.disable()
pr.print_stats(sort='time')
I run the script and get useful output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With