Code Comments
Programming Forum and web based access to our favorite programming groups.
On 24-Sep-2004, "JerryMouse" <nospam@bisusa.com> wrote:
> Today, however, any programmer who chooses a technique based on
> micro-efficiency without a compelling reason ("the boss told me to do it
> that way" is one), should be shot.
The definition of "compelling reason" is variable.
And micro-efficiency without significant a cost in understanding or
maintainability doesn't need a compelling reason.
Let's say the following occurs once per day.
Is there a reason to shoot the person who writes the 1st version of this cod
e,
saving 6 assembler moves per day?
MOVE THIS-DAY TO COMPARE-DAY
PERFORM VARYING DAY-INDEX FROM 1 BY 1 UNTIL DAY-INDEX > 7
IF COMPARE-DAY = DAY-OF-WEEK (DAY-INDEX)
PERFORM FOUND-ONE
END-IF
END-PERFORM
PERFORM VARYING DAY-INDEX FROM 1 BY 1 UNTIL DAY-INDEX > 7
MOVE THIS-DAY TO COMPARE-DAY
IF COMPARE-DAY = DAY-OF-WEEK (DAY-INDEX)
PERFORM FOUND-ONE
END-IF
END-PERFORM
Post Follow-up to this message"Howard Brazee" <howard@brazee.net> wrote in message news:cj9c26$dh9$1@peabody.colorado.edu... > Is there a reason to shoot the person who writes the 1st version of this code, > saving 6 assembler moves per day? No, but I'd consider shooting the guy who wrote the second one, which does not terminate the lookup (EXIT PERFORM) when COMPARE-DAY = DAY-OF-WEEK(DAY-INDEX) and performs FOUND-ONE. Curious how sometimes the most obvious optimizations are those most easily missed. MCM > > MOVE THIS-DAY TO COMPARE-DAY > PERFORM VARYING DAY-INDEX FROM 1 BY 1 UNTIL DAY-INDEX > 7 > IF COMPARE-DAY = DAY-OF-WEEK (DAY-INDEX) > PERFORM FOUND-ONE > END-IF > END-PERFORM > > > PERFORM VARYING DAY-INDEX FROM 1 BY 1 UNTIL DAY-INDEX > 7 > MOVE THIS-DAY TO COMPARE-DAY > IF COMPARE-DAY = DAY-OF-WEEK (DAY-INDEX) > PERFORM FOUND-ONE > END-IF > END-PERFORM
Post Follow-up to this messageOn 27-Sep-2004, "Michael Mattias" <michael.mattias@gte.net> wrote: > code, > > No, but I'd consider shooting the guy who wrote the second one, which doe s > not terminate the lookup (EXIT PERFORM) when COMPARE-DAY = > DAY-OF-WEEK(DAY-INDEX) and performs FOUND-ONE. > > Curious how sometimes the most obvious optimizations are those most easily > missed. I thought about that when I wrote this example - and decided that my point w as made better by leaving obvious ways to optimize the code without adding significant obfuscation/cost. It is interesting though that this "obvious" optimization is less obvious at closer examination. I don't know how efficient the compiler is, for instan ce, and I do know that adding an extra check does add some overhead. While my assumption is the same as yours - it is obvious that coding an early exit is more efficient than repeating the loop, I also see that 3 1/2 more iteration s isn't a big cost, and adding a comparison where a maintenance programmer mig ht make a mistake isn't quite as efficient as it looks. Still, we both code for efficiency when the cost appears low.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.