From: anon@att.net
Subject: Re: Assuming optimization? What is best of these code alternatives?
Date: Fri, 12 Sep 2014 06:17:30 +0000 (UTC)
Date: 2014-09-12T06:17:30+00:00 [thread overview]
Message-ID: <luu35p$fkp$1@speranza.aioe.org> (raw)
In-Reply-To: 0868c42e-ed44-4b36-a929-2bffb338ee34@googlegroups.com
Actually since the equation ( i1 + i2 .. n ) for the "for statement" is
evaluated once, this means that alternative 2 is the faster code.
Because in alternative 1 the system perform must reference ( K ) at
least once and possible twice plus provide storage for K.
Now, for optimization in Ada, The RM provide a "pragma Optimize" with
two options "Space" and "Time". But in GNAT "pragma Optimize" is
non-functional, instead GNAT uses GCC backend option -OX where X is
0 to 4 at the movement to perform optimization.
Now, since GNAT is a front-end to the compiler it is possible for
GNAT to use "pragma Optimize ( Space ) ;" to remove source-level dead
code. While "pragma Optimize ( Time ) ;" could turn on optimization in
the backend to a preset default optimization or allow command-line
option to override default.
Note: GCC does not have an option to remove dead code from precompile
libraries or routines during linking.
In <0868c42e-ed44-4b36-a929-2bffb338ee34@googlegroups.com>, reinkor <reinkor@gmail.com> writes:
>I am not sure to what extent one may assume the Ada compiler
>makes optimization. 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 ?
>
>Yes, this example is somehow over-simplified :-)
>
>
next prev parent reply other threads:[~2014-09-12 6:17 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
2014-09-12 6:17 ` anon [this message]
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