A script to cprofile a single function in python.

cprofile a single function in python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19

import logging
import cProfile
import pstats
import StringIO

from closure_chaining_example import smain

logging.basicConfig(format='%(asctime)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S', level=logging.DEBUG)
pr = cProfile.Profile()
pr.enable()
smain()
# do something
pr.disable()
s = StringIO.StringIO()
sortby = 'cumulative'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
logging.info("Profilestats: %s", s.getvalue())