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.

import cStringIO, cProfile, pstats

profile = cProfile.Profile()
profile.enable()

# run your code here 

profile.disable()
s = cStringIO.StringIO()
stats = pstats.Stats(profile, stream=s)
stats.strip_dirs()
stats.sort_stats(1)
stats.print_stats(20)

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.

import cStringIO, cProfile, pstats
import traceback

try:
    profile = cProfile.Profile()
    profile.enable()
    
    # run your code here 
    
    profile.disable()
    s = cStringIO.StringIO()
    stats = pstats.Stats(profile, stream=s)
    stats.strip_dirs()
    stats.sort_stats(1)
    stats.print_stats(20)
    
    print s.get_value()
except:
    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>