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

4

9

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?

Endgame answered 15/12, 2017 at 17:16 Comment(0)
A
8

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)
Ancestry answered 11/8, 2021 at 12:23 Comment(0)
M
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.

Marcomarconi answered 9/6, 2018 at 17:39 Comment(0)
U
1

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

Utas answered 25/7, 2023 at 14:40 Comment(0)
T
-3

Try python3 -m cProfile -s cumtime main.py

Toomay answered 12/7, 2018 at 2:43 Comment(1)
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. docs.python.org/3/library/profile.htmlLastex

© 2022 - 2024 — McMap. All rights reserved.