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!peer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!not-for-mail X-Trace: DXC=PN3ZR=JW7nDXhJNS8bZ=_G[3OhcoN[H0@X44`8^\]>7J8DHB_I2k0T@_WOU; SDD77DniX>DG5iBTDfZEAdbJ2; hE\LJBC307DeD3Ccl2TI3hNK X-Complaints-To: abuse@ngroups.net Date: Fri, 12 Sep 2014 06:32:12 +0200 From: Per Sandberg User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.7.0 MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Assuming optimization? What is best of these code alternatives? References: <0868c42e-ed44-4b36-a929-2bffb338ee34@googlegroups.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Message-ID: <5412774d$0$24718$862e30e2@ngroups.net> NNTP-Posting-Host: 463f8b06.ngroups.net X-Received-Bytes: 4281 X-Received-Body-CRC: 3500504042 Xref: number.nntp.dca.giganews.com comp.lang.ada:188977 Date: 2014-09-12T06:32:12+02:00 List-Id: Well It all depends on Compiler and target architecture so i would recommend the following actions i order. 1 Read the compilers manual on optimization flags. 2 Get yourself a decent analyzer. 3 If you are running gnat compile your program with "-O2" 4 Measure. 5 Analyze the results they are usually surprising. 5 Do code changes. 6 Back to 3 So the usual approach is 1) Write the program as readeble and understandable as possible. 2) Analyze the execution behavior on the real target with real data, 3) Optimize after deep analysis and reflection. The measurement tools usually points to parts of the code and constructs that are complete opposite of what you expected. /Good quotes J-P /Per On 11.09.2014 15:34, J-P. Rosen wrote: > 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." >