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 evilI 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 | |
Re: Re (VSarkiss)2: Optimizations and Efficiency
by scott (Chaplain) on Jun 30, 2001 at 18:26 UTC |