comp.lang.ada
 help / color / mirror / Atom feed
From: Stefan.Lucks@uni-weimar.de
Subject: Re: Assuming optimization? What is best of these code alternatives?
Date: Thu, 11 Sep 2014 20:12:07 +0200
Date: 2014-09-11T20:12:07+02:00	[thread overview]
Message-ID: <alpine.DEB.2.11.1409112001410.10305@debian> (raw)
In-Reply-To: <0868c42e-ed44-4b36-a929-2bffb338ee34@googlegroups.com>

[-- Attachment #1: Type: TEXT/PLAIN, Size: 1564 bytes --]

On Thu, 11 Sep 2014, reinkor wrote:

>    k := 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, 
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 
i1 nor i2 change, you could declare them as constants:

    declare
      I1: constant Some_Type := Get_I1;
      I2: constant Some_Type := 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!  ------
     <http://www.uni-weimar.de/cms/medien/mediensicherheit/home.html>
--Stefan.Lucks (at) uni-weimar.de, Bauhaus-Universität Weimar, Germany--

  parent reply	other threads:[~2014-09-11 18:12 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
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 [this message]
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