Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making cProf give only the 10 most time-consuming tasks or sort them by time in reverse order

Tags:

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?

like image 400
Caterina Avatar asked Dec 15 '17 17:12

Caterina


People also ask

How do you use a cProfile run?

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.

Which tool will analyze the data collected by the Python profiler?

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.

Which Python module can profile Python code in a sense to analyze its performance?

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).


1 Answers

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.

like image 115
Mikhail Golubitsky Avatar answered Sep 22 '22 12:09

Mikhail Golubitsky