Hi Ben,Mostly I just print to stdout, I imagine more flexibility would be needed in general.This is for python 2.7 - don't know if it works for 3.
def profile(sort='time', restriction=(), callers=None, callees=None, filename=None):
"print profile stats for decorated function"
def wrapper(*args, **kwargs): print 'Profile for:', func.__name__prof = cProfile.Profile()
result = prof.runcall(func, *args, **kwargs)
_, statsFileName = tempfile.mkstemp()
if filename is None:
stats = pstats.Stats(statsFileName)
stats = pstats.Stats(statsFileName, stream=open(filename, 'w'))
if isinstance(sort, basestring):
if isinstance(restriction, (tuple, list)):
if callers is not None:
if isinstance(callers, basestring):
if callees is not None:
if isinstance(callees, basestring):
return _profileDecoratorCheersTimOn 3 November 2016 at 09:58, Ben Hoyt <email@example.com> wrote:Okay, got it, that sounds fair enough. With your @profile decorator how do you tell it when and where to print the output? Can you post the source for your decorator?On Wed, Nov 2, 2016 at 4:52 PM, Tim Mitchell <firstname.lastname@example.org> wrote:I use an @profile() decorator for almost all my profiling. If you want to profile function foo you just decorate it and re-run the program.With a with block you have to find the places where foo is called and put with statements around the calls.I think both approaches are equally valid and useful.
Python-ideas mailing list
Code of Conduct: http://python.org/psf/