Scramblings

Dev scratchpad. Digital garden

Python Helpers - Profiling

Nov 9, 2019 | Reading Time: 1 min

A script to cprofile a single function in python.

CProfile a single function in python

 1import logging
 2import cProfile
 3import pstats
 4import StringIO
 5
 6from closure_chaining_example import smain
 7
 8logging.basicConfig(format='%(asctime)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S', level=logging.DEBUG)
 9pr = cProfile.Profile()
10pr.enable()
11smain()
12# do something
13pr.disable()
14s = StringIO.StringIO()
15sortby = 'cumulative'
16ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
17ps.print_stats()
18logging.info("Profilestats: %s", s.getvalue())