views:

69

answers:

2

When visual inspection fails, how do you determine where the slowest points in your code are? Where are the bottlenecks that are ruining your runtime?

+9  A: 

Profile your code.

The data obtained is irrefutable in determining the performance bottlenecks.

and the following allows you to visualize the profile data

Bunch of performance tips

Hidden performance bottleneck: Find shlemiel the painter inside your code

Joel Spolsky blogged on "Shlemiel the painter's algorithm" which succinctly describes the problem of scale in solutions.

pyfunc
Profiling isn't irrefutable, because profilers aren't perfect--but it's a very important tool. Granted, profilers are probably in general more accurate with Python than low-level languages, simply because high-level languages are so much slower that there's a lot more wiggle room for a profiler's overhead to not significantly alter the results.
Glenn Maynard
@Glenn Maynard : Agreed! I guess the language of my reply is a bit more enthusiastic.
pyfunc
Awesome response! Also, really interesting link on the python performance tips. Thanks!
coffee
A: 

I'll add to pyfunc's response.

You can profile python with some of the profilers available like cProfile.

Links about Python profiling: http://docs.python.org/library/profile.html

birryree