python code optimization
Peter Hansen
peter at engcorp.com
Fri Dec 30 11:03:56 EST 2005
Darius Kučinskas wrote:
> I know that sometimes optimization is not good idea.
It's neither good nor bad. Whether or not to optimize is merely a
decision that should not be made without considering the cost, and
without a real need driving it.
The need can only be seen by profiling your code and measuring
inadequate performance (which implies a standard by which you're judging
it).
The cost is not only the time it takes to optimize (which is often
small) but more importantly the effect on the readability and
maintainability of the code.
Most people don't bother measuring, and most ignore the readability
cost, and that's why most optimization is "premature" and why "sometimes
optimization is not a good idea" (if you really want to express it that
way).
> So I want to know what you think about this one:
...
> we optimize code like this:
> tables = []
> pfTablesExtend = tables.extend
> for i in ... :
> pfTablesExtend(...)
>
> I what to know is this bad idea? Say you opinion about that!
It's very easy to judge based on the guidance I gave above. Was the
unoptimized code really too slow? And does the result actually improve
the speed sufficiently? That is, did you measure before and after and
is the reduction in runtime significant? If the answer to any of these
questions is "no", then optimizing was a bad idea.
And does the value of the optimization outweigh the cost? In this case,
as a casual reader of that code (perhaps missing some context?), I would
say that reading "pfTablesExtend" surprises me, since I can't tell what
"pf" means, and there are no comments pointing out that the performance
without that extra stuff was insufficient.
If I were maintaining your code, I would probably hesitate a moment out
of uncertainty about why anyone would have done that in the first place,
and then I'd simply remove the optimization, assuming the programmer had
prematurely optimized. You can judge for yourself whether that makes
the time involved in optimizing this worth it. ;-)
--
Peter
More information about the Python-list
mailing list