snippet.profile_sampleΒΆ

A simple decorator for profiling.

This is based on the implementation of Expert Python Programming by Tarek Ziade. Code referred from p.330 in Japanese translation.

How to profile a function

import time
def med():
    time.sleep(0.01)

def lgt():
    time.sleep(0.001)

def hvy():
    print 'heavy process...'
    for i in range(100):
        lgt()
        med()
        med()
    time.sleep(0.5)

@profile('A test for main', list=6)
def main():
        for i in range(2):
            hvy()

print 'Using as a decorator'
main()

print 'Using as a normal function'
p = profile()(med)
p()
''' A simple decorator for profiling.

This is based on the implementation of Expert Python Programming
by Tarek Ziade.  Code referred from p.330 in Japanese translation.

How to profile a function

.. code-block:: python

    import time
    def med():
        time.sleep(0.01)

    def lgt():
        time.sleep(0.001)

    def hvy():
        print 'heavy process...'
        for i in range(100):
            lgt()
            med()
            med()
        time.sleep(0.5)

    @profile('A test for main', list=6)
    def main():
            for i in range(2):
                hvy()

    print 'Using as a decorator'
    main()

    print 'Using as a normal function'
    p = profile()(med)
    p()
'''

import tempfile
import os
import cProfile
import pstats

from irfpy.util.profile import profile

def main_profile():
    import time

    def med():
        time.sleep(0.01)

    def lgt():
        time.sleep(0.001)

    def hvy():
        print('heavy process...')
        for i in range(100):
            lgt()
            med()
            med()
        time.sleep(0.5)

    @profile('A test for main', list=6)
    def main():
            for i in range(2):
                hvy()

    print('Using as a decorator')
    main()

    print('Using as a normal function')
    p = profile()(med)
    p()

if __name__ == '__main__':
    main_profile()