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: border2.nntp.dca1.giganews.com!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!news-1.dfn.de!news.dfn.de!news.uni-weimar.de!medsec1.medien.uni-weimar.de!lucks From: Stefan.Lucks@uni-weimar.de Newsgroups: comp.lang.ada Subject: Re: Assuming optimization? What is best of these code alternatives? Date: Thu, 11 Sep 2014 20:12:07 +0200 Organization: Bauhaus-Universitaet Weimar Message-ID: References: <0868c42e-ed44-4b36-a929-2bffb338ee34@googlegroups.com> NNTP-Posting-Host: medsec1.medien.uni-weimar.de Mime-Version: 1.0 Content-Type: MULTIPART/MIXED; BOUNDARY="8323329-752378436-1410458907=:10305" X-Trace: pinkpiglet.scc.uni-weimar.de 1410452035 19964 141.54.178.228 (11 Sep 2014 16:13:55 GMT) X-Complaints-To: news@pinkpiglet.scc.uni-weimar.de NNTP-Posting-Date: Thu, 11 Sep 2014 16:13:55 +0000 (UTC) X-X-Sender: lucks@debian In-Reply-To: <0868c42e-ed44-4b36-a929-2bffb338ee34@googlegroups.com> User-Agent: Alpine 2.11 (DEB 23 2013-08-11) Content-ID: Xref: number.nntp.dca.giganews.com comp.lang.ada:188971 Date: 2014-09-11T20:12:07+02:00 List-Id: This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323329-752378436-1410458907=:10305 Content-Type: TEXT/PLAIN; CHARSET=ISO-8859-15; FORMAT=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Content-ID: On Thu, 11 Sep 2014, reinkor wrote: > k :=3D i1 + i2; -- store "i1 + i2" in k to avoid recalculation (?) > loop > for i in k .. n loop > Do something... > end loop; > end loop; > loop > for i in i1 + i2 .. n loop -- will recalculate "i1 + i2" ? > Do something... > end loop; > end loop; > > What is the "best" alternative? I assume you turn on the compiler's optimisation setting. I didn't try it,= =20 but I would expect the following: - If "Do something" is sufficiently simple, the Ada compiler will find out that i1, i2 and, for the first variant, k, don't change. Then the generated code should be the same. - If "Do something" is so complex that the Ada compiler is unable to find out that neither of i1, i2, and k change, you are unlikely to notice any difference in speed, since the time to compute i1+i2 is will be negligible, compared to the time for "Do something". Nevertheless, if you want to make sure the compiler notices that neither=20 i1 nor i2 change, you could declare them as constants: declare I1: constant Some_Type :=3D Get_I1; I2: constant Some_Type :=3D Get_I2; begin loop for I in I1+I2 .. N loop Do_Something end loop; end loop; end; ------ I love the taste of Cryptanalysis in the morning! ------ --Stefan.Lucks (at) uni-weimar.de, Bauhaus-Universit=E4t Weimar, Germany-- --8323329-752378436-1410458907=:10305--