in reply to Timing of Array-Size Determination Methods

Your benchmark is sound, but look at what it revealed... sure, you can save 1.5 microseconds by dropping the word "scalar", but even if you executed that one million times, that's still less than two seconds of time, and if it helps a programmer figure out what the code is doing to save those two seconds, you're still ahead.

Use what's clear. Do not prematurely optimize. Let your default be to optimize for programmer understanding.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

  • Comment on •Re: Timing of Array-Size Determination Methods

Replies are listed 'Best First'.
Re: •Re: Timing of Array-Size Determination Methods
by halley (Prior) on Jun 02, 2003 at 16:53 UTC

    While I completely agree with your sentiment, sometimes it's not premature. Prematurely scolding about premature optimization is, well, premature.

    If a snippet of code is utilized in some tight loop, perhaps it IS warranted, and knowing the difference was useful information. It's only 1.5 microseconds, but it's 50% of that particular comparison after Benchmark amortizes the sub calls. That MAY be significant to SOME loops.

    While one really shouldn't rely on Perl for hyper-tight loops, sometimes you still want to knock a second or two off of an otherwise sound algorithm. And in the case of @foo vs scalar @foo, I'd say any usefully-competent Perl maintainer will know what's meant by the code.

    --
    [ e d @ h a l l e y . c c ]

Re: •Re: Timing of Array-Size Determination Methods
by Itatsumaki (Friar) on Jun 02, 2003 at 17:12 UTC

    Fair enough, but in my mind:

    1. I would have never known if there was a significant difference w/o doing the benchmark
    2. Even if the difference isn't of practical importance, understanding why it happens can be a great learning tool

    -Tats
      Given how small the amounts of time are that we're talking about here, I don't think you could call this difference significant, except in the statistical sense.