Writing code to be optimizable

Roy Smith roy at panix.com
Wed Nov 23 08:31:57 EST 2011


In article 
<63e78437-c76b-4a9e-9a62-bfea8d078208 at v5g2000yqn.googlegroups.com>,
 snorble <snorble at hotmail.com> wrote:

> Is it reasonable to prototype an application in Python that will
> require performance?

Yes.  Several observations:

1) The classic 80/20 rule.  80% of the time is spent running 20% of the 
code.  Sometimes quoted as 90/10.  Why waste time optimizing the 80%?

2) Surprisingly to many people, it's difficult to predict in advance 
which 20% will be the slow parts.  Don't sweat it until the whole thing 
is up and running and you can begin to gather profiling data.

3) Often enough, when you're done writing your program, you'll discover 
that it's fast enough to get value from.  Be happy and move onto the 
next task on your list.

> Are there any recommendations on how to write code in such a way that 
> it can later be optimized or replaced with a module written in C or 
> Cython?

Sure.  Make your code modular and loosely coupled.  Each module or class 
should do one thing, and have narrow, well-defined interfaces.  Limit 
the number of other modules or classes it interacts with directly.  If 
you've done that, it becomes easier to a) identify the slow parts and b) 
plug something better in its place.



More information about the Python-list mailing list