Which happens first?

Steve Holden sholden at holdenweb.com
Sun Apr 8 12:54:02 EDT 2001


"Remco Gerlich" <scarblac at pino.selwerd.nl> wrote in message
news:slrn9d057b.llp.scarblac at pino.selwerd.nl...
> Carlos Alberto Reis Ribeiro wrote in comp.lang.python:
>  > My personal conclusions are as follows. Please feel free to disagree
and/or
> > clarify:
> >
> > 1) Avoid any unnecessary calculation, specially inside tight loops. I
tend
> > to use lots of explicit calculations, in order to make code easier to
> > understand. Things like adding/subtracting one, or calculating an offset
> > (for a struct, for instance) are common applications of this idiom, for
> > example:
> >
> >  >>> p = a[(3*4+2)-1]   # get the second byte of the third DWORD
> >
> > This kind of construct should be avoided.
>
> Premature optimization is one of the worst programming sins.
>
This sin is also known as "introducing implementation bias". Imagine how
silly you will feel if you spend several days optimizing a large Python
suite in this way, then changes in the compiler technology render your code
even less efficient than it would have benn if written naturalistically.

> You spend programmer time (figuring out which order to type it) and
> readability (writing it (3*4+2)-1 is self-documenting) in order to get
some
> tiny speed plus that probably doesn't matter at all.
>
If speed matters as much as Carlos seems to feel it does then arguably
Python isn't the right language in the first place! Of course, as
suggestions for how the compiler might do a better job they are fine, but
I'm sure the developers would be happier to see patches than suggestions.

> Only when your finished program is too slow, you can start optimizing.
First
> you use the profiler to find the 2% of your code that does all the slow
> work. Then you improve the algorithm. And if that didn't work, usually
> rewriting that small bit in C will be much more effective than saving a
> bytecode because of the order of the operands...
>
Yup. First, make it work. THen (if it doesn't work fast enough) make it work
faster.

the-wrong-answer-in-half-the-time-is-worthless-ly y'rs  - steve






More information about the Python-list mailing list