comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adambeneschan@gmail.com>
Subject: Re: Assuming optimization? What is best of these code alternatives?
Date: Thu, 11 Sep 2014 07:48:07 -0700 (PDT)
Date: 2014-09-11T07:48:07-07:00	[thread overview]
Message-ID: <6eb0f75b-9885-4b09-8e7f-6802d0c39d56@googlegroups.com> (raw)
In-Reply-To: <lus8di$1ih$1@dont-email.me>

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+i2 does need to be recalculated for every iteration of the outer loop.  However, the OP has stated that i1 and i2 will not change, and his proposed initialization takes place outside the outer loop.

The question is, if i1 and i2 won't change, will the compiler optimize so that it doesn't have to re-perform the addition in each iteration of the outer loop.  The only answer is to try it with your compiler and see.

                                  -- Adam



  reply	other threads:[~2014-09-11 14:48 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 ` Assuming optimization? What is best of these code alternatives? J-P. Rosen
2014-09-11 14:48   ` Adam Beneschan [this message]
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