Title: Add more kwargs to cProfile's print_stats
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Thane Brimhall, ethan.furman
Priority: normal Keywords:

Created on 2017-01-11 03:00 by Thane Brimhall, last changed 2017-01-11 03:53 by ethan.furman.

Messages (1)
msg285183 - (view) Author: Thane Brimhall (Thane Brimhall) * Date: 2017-01-11 03:00
Using the pstats class to print off profiler results is helpful when you want to filter, order, and limit the number of returned lines. I'd rather see the most commonly-used subset of pstats functionality included in the profiler's print_stats implementation directly.

    # The current way

    # Proposed way
    profiler.print_stats(strip_dirs=False, sort='cumulative', reverse=True, limit=10)

Currently only the `sort` kwarg is available. To me this implies that some level of control was originally intended to be available in the print_stats method anyway. I also feel like the proposed API is more readable and explicit.

Note that for complex situations you'd still need to use the pstats class directly, eg. substituting a different stream implementation or filtering/sorting by multiple values.

This would be a backwards-compatible patch and would be implemented something like this:

    def print_stats(self, sort=-1, limit=None, strip_dirs=True, reverse=True):
        import pstats
        stats = pstats.Stats(self)
        if strip_dirs:
            stats = stats.strip_dirs()
        stats = stats.sort_stats(sort)
        if reverse:
            stats = stats.reverse_order()
Date User Action Args
2017-01-11 03:53:36ethan.furmansetnosy: + ethan.furman
2017-01-11 03:00:31Thane Brimhallcreate