comp.lang.ada
 help / color / mirror / Atom feed
From: "J-P. Rosen" <rosen@adalog.fr>
Subject: Re: Assuming optimization? What is best of these code alternatives?
Date: Thu, 11 Sep 2014 15:34:41 +0200
Date: 2014-09-11T15:34:41+02:00	[thread overview]
Message-ID: <lus8di$1ih$1@dont-email.me> (raw)
In-Reply-To: <0868c42e-ed44-4b36-a929-2bffb338ee34@googlegroups.com>

Le 11/09/2014 15:14, reinkor a écrit :
> I am not sure to what extent one may assume the Ada compiler 
> makes optimization.
It does. An Ada compiler without optimization would not be usable.
(and I worked on Ada/ED !)

> For example, assume the following two program 
> code alternatives:
> 
> 
> -----------------------Alternative 1:
>    i1,i2,k : Integer;
> begin
>     (Read i1,i2 and n - do not change after)
> 
>     k := i1 + i2; -- store "i1 + i2" in k to avoid recalculation (?)
>     loop
>       for i in k .. n loop
>           Do something...
>       end loop;
>     end loop;
> 
> ------------------------Alternative 2:
>    i1,i2 : Integer;
> begin
>     (Read i1, i2 and n - do not change after)
> 
>     loop
>       for i in i1 + i2 .. n loop -- will recalculate "i1 + i2" ?
>           Do something...
>       end loop;
>     end loop;
> 
> What is the "best" alternative?  Alternative 2 
> is shortest (and probably easier to read), but
> could it give slower code that for Alternative 1 ?
> 
Response 1: don't bother with such micro-optimization
Response 2: the language requires that the bounds be evaluated only
once, not on each loop. Therefore, it won't make any difference.

FWIW, here is my favorite collection of quotes about optimization:

Kernighan & Plauger, 1974.  Elements of Programming Style:
- Make it right before you make it faster.
- Keep it right when you make it faster.
- Make it clear before you make it faster.
- Don't sacrifice clarity for small gains in "efficiency."
- Let your compiler do the simple optimizations.
- Keep it simple to make it faster.
- Don't diddle code to make it faster - find a better algorithm.
- Instrument your programs.  Measure before making "efficiency" changes.

Ledgard, Nagin, Hueras, 1979. Pascal with Style: Programming Proverbs:
Shortening the code, running the program faster, or using fewer
variables are all popular pastimes. Not mentioning ... the extra testing
time needed to check the new and often subtle boundary conditions, are
you sure that fewer machine instructions or faster machine execution is
likely?

M.A. Jackson
Rules of Optimization:
- Rule 1: Don't do it.
- Rule 2 (for experts only): Don't do it yet.

W.A. Wulf
"More computing sins are committed in the name of efficiency (without
necessarily achieving it) than for any other single reason - including
blind stupidity."

Donald Knuth
"We should forget about small efficiencies, say about 97% of the time:
premature optimization is the root of all evil."

-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr


       reply	other threads:[~2014-09-11 13:34 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <0868c42e-ed44-4b36-a929-2bffb338ee34@googlegroups.com>
2014-09-11 13:34 ` J-P. Rosen [this message]
2014-09-11 14:48   ` Assuming optimization? What is best of these code alternatives? Adam Beneschan
2014-09-12  4:32   ` Per Sandberg
2014-09-11 14:19 ` gautier_niouzes
2014-09-11 14:49   ` Adam Beneschan
2014-09-11 17:30 ` Jeffrey Carter
2014-09-11 18:12 ` Stefan.Lucks
2014-09-12  6:17 ` anon
2014-09-12  8:06   ` reinkor
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox