8

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?

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Caterina
  • 775
  • 9
  • 26

4 Answers4

5

To print the 10 most time-consuming tasks of a function called "function_to_profile", you can run the following:

if __name__ == "__main__":
    import cProfile
    from pstats import Stats

    pr = cProfile.Profile()
    pr.enable()

    function_to_profile()

    pr.disable()
    stats = Stats(pr)
    stats.sort_stats('tottime').print_stats(10)
LadyChristina
  • 86
  • 1
  • 3
4

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.

Mikhail Golubitsky
  • 590
  • 1
  • 8
  • 10
0

You can also use the Linux head command as follows:

  • Also, it's recommended to use cumtime for sorting.
python -m cProfile -s cumulative main.py | head -n 15

-n 15: This tells you how many lines of the cProfile output you need. By choosing 15, it will show the top 10 code lines (5 for formatting and reports of cProfile)."

Mo Abbasi
  • 11
  • 2
-2

Try python3 -m cProfile -s cumtime main.py

Jason
  • 2,950
  • 2
  • 30
  • 50
  • This will print the profile based on the cumulative time but in descending order. I think Mikhail is write in saying that it is only possible with an internal solution to invoke the Stats class. https://docs.python.org/3/library/profile.html – mike Oct 03 '19 at 21:54