in reply to Re (tilly) 1: Optimizations and Efficiency
in thread Optimizations and Efficiency

tilly, I wish I had ++ points left today. You've made excellent points; I agree with you 100%. If I had to pick one statement from McConnell's book that you linked to, it would be the quote from Donald Knuth1:

Premature optimization is the root of all evil
I have just this to add: If your code is running slowly, don't guess where the slow spot is, measure. Far too many times, I've seen people confronted by a performance problem take off and try to optimize what they thought was the slow part, instead of trying to profile the program to validate their belief. Then, after reasonable meausrements are made, it turns out their optimizations just made the code complicated and hard to maintain, without making a dent in its run time.

I remember reading about a study, where programmers, asked to guess where they thought a slow program was spending its time, guessed wrong 80% of the time. (I can't seem to find a reference; perhaps someone here can compensate for my Alzheimer's?)

Here's an exercise: suppose I want to add all the integers between n and m. Try guessing which of these will run faster, then use Benchmark to validate your guess. The results will probably surprise you.

use constant N => 1; # for example use constant M => 10_000; # for example my $sum; # Method 1: a simple for loop. $sum = 0; for (my $i = N; $i <= M; $i++) { $sum += $i; } # Method 2: an evil use of map by throwing away its result. $sum = 0; map { $sum += $_ } N..M; # Method 3: the APL way $sum = eval join('+', N..M);

1Hmm, I just realized how strange that sounds; my pick from one author's book is a quote from someone else.

Replies are listed 'Best First'.
Re: Optimizations and Efficiency
by petdance (Parson) on Jun 30, 2001 at 07:50 UTC
    Premature optimization is the root of all evil

    A favorite of mine, too, but is it even Knuth? I've seen it attributed to at least two other authors as well.

    Regardless, here's my big question:

    Do you even NEED to optimize it?

    So many programmers sit and twiddle their bits in either the wrong place, or in entirely the wrong code.

    • If the program's not slow, it doesn't need optimizing.
    • If the program's run only occassionally, it probably doesn't need optimizing.
    • If it's not impacting anything by being slow, it doesn't need optimizing.
    Bit-twiddling is fun, but for those of us who are professional programmers, there's no reason to waste our valuable time squeezing cycles from the program Just Because It's There.

    xoxo,
    Andy
    --
    I was dreaming when I wrote this, so sue me if I go too fast.

Re: Re (VSarkiss)2: Optimizations and Efficiency
by scott (Chaplain) on Jun 30, 2001 at 18:26 UTC

    For further fun try:

    # Method 3a: the vaguely Mathematica-ish way $sum = 0 + join('+', N..M);
    which generates different results, on my machine anyway.

    One ought to also compare push, pop, shift, and unshift as well as while, for, foreach, do, and, shudder, a hand crafted LABEL loop.