Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Python profile decorator (See related posts)

Python profile decorator. More info on this blog post: http://www.biais.org/blog/index.php/2007/01/20/18-python-profiling-decorator

# Maxime Biais <http://www.biais.org/blog>

import hotshot, hotshot.stats
 
def profileit(printlines=1):
    def _my(func):
        def _func(*args, **kargs):
            prof = hotshot.Profile("profiling.data")
            res = prof.runcall(func, *args, **kargs)
            prof.close()
            stats = hotshot.stats.load("profiling.data")
            stats.strip_dirs()
            stats.sort_stats('time', 'calls')
            print ">>>---- Begin profiling print"
            stats.print_stats(printlines)
            print ">>>---- End profiling print"
            return res
        return _func
    return _my


Usage:
@profileit(20)
def mop():
    a = 0
    for i in range(100):
        a += mip()
    return a
print mop()

You need to create an account or log in to post comments to this site.


Click here to browse all 5140 code snippets

Related Posts