3

I am using cProfile to profile the leading function of my entire app. It's working great except for some 3rd party libraries that are being profiled, and shown in the output as well. This is not always desirable when reading the output.

My question is, how can i limit this? The documentation on python profilers mentions:

p.sort_stats('time', 'cum').print_stats(.5, 'init')

This line sorts statistics with a primary key of time, and a secondary key of cumulative time, and then prints out some of the statistics. To be specific, the list is first culled down to 50% (re: .5) of its original size, then only lines containing init are maintained, and that sub-sub-list is printed.

It mentions that lines containing "init" will only be displayed. This works great, but i want to limit it in such a way so that i can say, limit lines containing "init" and/or "get". By trial and error i found that you can add another argument, a string, and it will be filtered further to display only lines containing string1 and string2. Using my previous example, this would look like:

p.sort_stats('time', 'cum').print_stats(.5, 'init', 'get')

Anyone know how you could limit the lines so that only lines containing "init" and/or "get" would be displayed?

Lee Olayvar
  • 3,660
  • 6
  • 30
  • 36

1 Answers1

2

Per the docs you linked, the strings are regular expressions:

Each restriction is either an integer ..., or a regular expression (to pattern match the standard name that is printed; as of Python 1.5b1, this uses the Perl-style regular expression syntax defined by the re module)

Accordingly:

p.sort_stats('time', 'cum').print_stats(.5, 'init|get')

should work.

brian-brazil
  • 31,678
  • 6
  • 93
  • 86