Optimisation

Paul Makepeace paulm at paulm.com
Tue Mar 3 12:46:27 GMT 2009


On Tue, Mar 3, 2009 at 12:17 PM, Nigel Peck <nigel.peck at miswebdesign.com> wrote:
> Mark Morgan wrote:
>>
>> On Mon, Mar 2, 2009 at 9:01 PM, Nigel Peck <nigel.peck at miswebdesign.com>
>> wrote:
>>>
>>> When the list could be empty, which is faster?
>>>
>>> if ( @list ) { foreach ( @list ) { } }
>>>
>>> - or just -
>>>
>>> foreach ( @list ) { }
>>>
>>> Or is it a pointless question?
>>
>> Sounds like a definite case of premature optimisation.  If it's not
>> causing issues for production code, ignore it.  If it is, then there's
>> very likely much bigger problems....
>
> I'm just asking because I try to think about optimisation as I'm writing
> code, rather than going back and doing it later, which I've only done in
> rare cases.
>

It's more important to get your algorithms right than this kind of
edge-case peephole work.

It's also important in a different way to write code that doesn't
leave other developers (or yourself, six months from now) looking at,

  if (@list) {
    foreach my $element (@list) {
       ...

and thinking "WTF?!" (because that is a very reasonable reaction to
seeing that code)

P



More information about the london.pm mailing list