Skip to content

Latest commit

 

History

History
37 lines (32 loc) · 1.34 KB

File metadata and controls

37 lines (32 loc) · 1.34 KB

Python profiling function

This is a special function definition that uses cProfile module to do code profiling on the given function. After you define it, use profiling function with profiling decorator

import cProfile, pstats, io
from pstats import SoftKey

def runProfiler( function ):
   def inner(*args, **kwargs):
      pr = cProfile.Profile()
      pr.enable()
      retval = function(*args, **kwargs)
      pr.disable()
      s = io.StringIO()
      sortby = SortKey.CUMULATIVE
      ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
      ps.print_stats()
      print(s.getvalue())
      return retval
   return inner
see also
references

https://docs.python.org/3/library/profile.html#profile.Profile