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,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.182.171.67 with SMTP id as3mr727747obc.34.1410446888511; Thu, 11 Sep 2014 07:48:08 -0700 (PDT) X-Received: by 10.50.50.205 with SMTP id e13mr33958igo.17.1410446888421; Thu, 11 Sep 2014 07:48:08 -0700 (PDT) Path: border1.nntp.dca1.giganews.com!nntp.giganews.com!r10no169451igi.0!news-out.google.com!ef6ni2722igb.0!nntp.google.com!r10no169449igi.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Thu, 11 Sep 2014 07:48:07 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=66.126.103.122; posting-account=KSa2aQoAAACOxnC0usBJYX8NE3x3a1Xq NNTP-Posting-Host: 66.126.103.122 References: <0868c42e-ed44-4b36-a929-2bffb338ee34@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <6eb0f75b-9885-4b09-8e7f-6802d0c39d56@googlegroups.com> Subject: Re: Assuming optimization? What is best of these code alternatives? From: Adam Beneschan Injection-Date: Thu, 11 Sep 2014 14:48:08 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: number.nntp.dca.giganews.com comp.lang.ada:188967 Date: 2014-09-11T07:48:07-07:00 List-Id: On Thursday, September 11, 2014 6:34:44 AM UTC-7, J-P. Rosen wrote: > > 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; > 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. I think you missed the outer loop. The for loop is an inner loop, and i1+i= 2 does need to be recalculated for every iteration of the outer loop. Howe= ver, the OP has stated that i1 and i2 will not change, and his proposed ini= tialization takes place outside the outer loop. The question is, if i1 and i2 won't change, will the compiler optimize so t= hat it doesn't have to re-perform the addition in each iteration of the out= er loop. The only answer is to try it with your compiler and see. -- Adam