python cprofile shows lot of information. Can it be limited to only my code
Asked Answered
S

3

6

cProfile shows lot of built-in function calls in the output. Can we limit the output only to the code I have written. So in the below example, can i see only the lines from testrun or the functions called from testrun() which resides in the same script. Or may be limit the level of calls logged to 2 or 3 ?

pr = cProfile.Profile()
pr.enable()
testrun()
pr.disable()
pr.print_stats(sort='time')
Sweep answered 21/9, 2021 at 7:40 Comment(0)
M
4

You can filter the output as shown in this question.

So, for example, you could filter by your modules name, c.f., print_stats():

pr.sort_stats("time").print_stats("dir_or_module_name")  # replace dir_or_module_name

Just make sure not to call strip_dirs() before, as that would possibly remove your directory/module name.


Edit: you can even filter by multiple names, as print_stats() should accept a regex:

pr.print_stats("dir1|dir2|module1")
Marivelmariya answered 9/11, 2021 at 9:10 Comment(0)
T
1

Not sure what version of python Valentin Khun's answer is for, but in Python 3.12.2 I had to use a combination of cProfile and pstats to filter my results.

Here is an example showing how to limit to your code, while also excluding private and magic callables.

MRE

from cProfile import Profile
from pstats import Stats
from time import sleep


def hello() -> None:
    print("Hello World!")
    sleep(1)


def _private() -> None:
    some_list = []
    for i in range(10):
        some_list.append(i)


def main() -> None:
    hello()
    _private()


if __name__ == '__main__':
    # Using ``Profile`` as a context manager.
    # https://docs.python.org/3.12/library/profile.html#profile.Profile
    with Profile() as pr:
        main()

    stats = Stats(pr).sort_stats("cumtime")
    stats.print_stats(r"\((?!\_).*\)$")  # Exclude private and magic callables.

Output

...\scratches\scratch.py 
Hello World!
         17 function calls in 1.000 seconds

   Ordered by: cumulative time
   List reduced from 8 to 2 due to restriction <'\\((?!\\_).*\\)$'>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    1.000    1.000 ...\scratches\scratch.py:17(main)
        1    0.000    0.000    1.000    1.000 ...\scratches\scratch.py:6(hello)

Tajuanatak answered 24/4, 2024 at 17:48 Comment(0)
A
0

An alternative if you use the command line to generate the profiling results like i did is to do:

$ python -m pstats your_file_name.profile 

to launch pstat interactive mode, and then:

$ stats <dir you want to filter by>
Aleras answered 4/7, 2024 at 14:38 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.