Skip to content

Instantly share code, notes, and snippets.

@nealtodd
Created April 25, 2012 13:15
Show Gist options
  • Select an option

  • Save nealtodd/2489618 to your computer and use it in GitHub Desktop.

Select an option

Save nealtodd/2489618 to your computer and use it in GitHub Desktop.

Revisions

  1. nealtodd revised this gist Apr 25, 2012. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions decorator.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    from cProfile import Profile
    import pstats


    def profile(sort_args=['cumulative'], print_args=[10]):
    profiler = Profile()

  2. nealtodd revised this gist Apr 25, 2012. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions example.py
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,14 @@
    """
    Python decorator for profiling a function using cProfile. Stats are output to wherever print statements would normally go to, e.g. stdout or a log file. Can be used with Django view functions.
    Python decorator for profiling a function using cProfile. Stats are output to wherever print statements would
    normally go to, e.g. stdout or a log file. Can be used with Django view functions.
    pstats' sort and print arguments can be supplied to the decorator. The default is to sort cumulatively and list to first 10 lines.
    pstats' sort and print arguments can be supplied to the decorator. The default is to sort cumulatively and
    list to first 10 lines.
    See http://docs.python.org/library/profile.html#instant-user-s-manual for details on pstats sort and print arguments.
    cProfile is a standard module in most python distributions. pstats is also in many but not all. If pstats doesn't import then install the module via the python-profiler package. E.g. on apt-based systems:
    cProfile is a standard module in most python distributions. pstats is also in many but not all. If pstats doesn't
    import then install the module via the python-profiler package. E.g. on apt-based systems:
    sudo apt-get install python-profiler
    """

  3. nealtodd created this gist Apr 25, 2012.
    16 changes: 16 additions & 0 deletions decorator.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    from cProfile import Profile
    import pstats
    def profile(sort_args=['cumulative'], print_args=[10]):
    profiler = Profile()

    def decorator(fn):
    def inner(*args, **kwargs):
    result = None
    try:
    result = profiler.runcall(fn, *args, **kwargs)
    finally:
    stats = pstats.Stats(profiler)
    stats.strip_dirs().sort_stats(*sort_args).print_stats(*print_args)
    return result
    return inner
    return decorator
    22 changes: 22 additions & 0 deletions example.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    """
    Python decorator for profiling a function using cProfile. Stats are output to wherever print statements would normally go to, e.g. stdout or a log file. Can be used with Django view functions.
    pstats' sort and print arguments can be supplied to the decorator. The default is to sort cumulatively and list to first 10 lines.
    See http://docs.python.org/library/profile.html#instant-user-s-manual for details on pstats sort and print arguments.
    cProfile is a standard module in most python distributions. pstats is also in many but not all. If pstats doesn't import then install the module via the python-profiler package. E.g. on apt-based systems:
    sudo apt-get install python-profiler
    """

    # Example without profiling arguments:

    @profile():
    def my_function(my_args):
    pass

    # Example with profiling arguments:

    @profile(sort_args=['cumulative', 'name'], print_args=[.5, 'init']):
    def my_function(my_args):
    pass