From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: border1.nntp.dca1.giganews.com!nntp.giganews.com!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "J-P. Rosen" Newsgroups: comp.lang.ada Subject: Re: Assuming optimization? What is best of these code alternatives? Date: Thu, 11 Sep 2014 15:34:41 +0200 Organization: A noiseless patient Spider Message-ID: References: <0868c42e-ed44-4b36-a929-2bffb338ee34@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Thu, 11 Sep 2014 13:34:42 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="23dab0694e4174fdc880833ec67fa650"; logging-data="1617"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/d2OyzuA5rBoQeURW56ZOY" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: <0868c42e-ed44-4b36-a929-2bffb338ee34@googlegroups.com> Cancel-Lock: sha1:JUd/v0BZUlPArFQJECFGzTH/kSE= Xref: number.nntp.dca.giganews.com comp.lang.ada:188964 Date: 2014-09-11T15:34:41+02:00 List-Id: 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