Trivial performance questions

Alex Martelli aleax at aleax.it
Sat Oct 18 09:52:57 EDT 2003


Peter Hansen wrote:

> Geoff Gerrietts wrote:
>> 
>> Maybe I'm a heretic, but I think this is a healthy attitude to have.
>> If you can write it optimally the first time with no significant
>> increase in effort, then nobody's going to hafta come back and rewrite
>> it later: that's a big maintenance win.
> 
> Not unless you add Alex' constraint that the two alternatives under
> consideration are equally readable.  Otherwise the less readable one
> is always going to cost you more at maintenance time.  And I'd add
> my own constraint that you actually have to *need* the speed.  Otherwise
> even the "insignificant" increase in effort that it will cost you will
> not be paying for itself.

If you *get into the habit* of always checking with "if x is None:"
rather than "if x == None:" -- two equally readable constructs -- it
will cost you no increase in effort whatsoever to always use the
idiom you've gotten used to.  So, whether the (tiny) extra speed and
readability are important or not, it's still a good habit to pick up.

I would (more hesitantly) claim the same for ''.join vs += in putting
"a few" strings together.  If they're really just a few performance
will not matter.  But as soon as they're not so few it does suddenly
matter A LOT.  So, even though the effort may not be trivial in this
case, I still think of this as "a good habit to pick up" to the point
where it becomes automatic... but it's not _quite_ as clear-cut.


Alex





More information about the Python-list mailing list