3

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')
sjd
  • 1,329
  • 4
  • 28
  • 48

1 Answers1

2

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")
Valentin Kuhn
  • 753
  • 9
  • 25