Profile your code

share on google plus share on facebook share on twitter share on linkedin share via email

Research in the field of Structural Design is more about developing concepts than implementations. If a concept is sound, the details of the implementation can be figured out later, by someone with more computational and/or programming experience.

Of course, while developing concepts and approaches, we need some kind of implementation to test and prove they actually work. And, let’s face it, we all like writing reasonably fast and robust implementations. Therefore, at one point or another, we all rewrite code to optimise and make it run faster.

In my experience, the slowest part of your code is almost never the part you imagined. Profiling your code before optimising will save you a lot of work.

Profiling Python code with the Python profilers is easy. A detailed explanation can be found here. Below is a recipe that always worked well for me.

01.import cStringIO, cProfile, pstats
02. 
03.profile = cProfile.Profile()
04.profile.enable()
05. 
06.# run your code here
07. 
08.profile.disable()
09.s = cStringIO.StringIO()
10.stats = pstats.Stats(profile, stream=s)
11.stats.strip_dirs()
12.stats.sort_stats(1)
13.stats.print_stats(20)
14. 
15.print s.get_value()

If you run the profile as part of a standalone script and you want to provide feedback on what went wrong during a failed execution, you can use the traceback module.

01.import cStringIO, cProfile, pstats
02.import traceback
03. 
04.try:
05.    profile = cProfile.Profile()
06.    profile.enable()
07.     
08.    # run your code here
09.     
10.    profile.disable()
11.    s = cStringIO.StringIO()
12.    stats = pstats.Stats(profile, stream=s)
13.    stats.strip_dirs()
14.    stats.sort_stats(1)
15.    stats.print_stats(20)
16.     
17.    print s.get_value()
18.except:
19.    print traceback.format_exc()

Leave a response

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>