From: "Jeffrey R. Carter" <spam.jrcarter.not@spam.acm.org>
Subject: Re: Is there an Ada compiler whose Ada.Numerics.Generic_Elementary_Functions.Log(Base=>10, X=>variable) is efficient?
Date: Mon, 15 Feb 2010 16:04:34 -0700
Date: 2010-02-15T16:04:34-07:00 [thread overview]
Message-ID: <hlcko7$6r7$1@tornado.tornevall.net> (raw)
In-Reply-To: <alpine.LNX.2.00.1002151055530.17315@Bluewhite64.example.net>
Colin Paul Gloster wrote:
> with Ada.Numerics.Generic_Elementary_Functions;
> with Interfaces.C;
> with Ada.Text_IO;
> procedure Logarithmic_Work_In_Ada is
> answer : Interfaces.C.double := 0.0;
> package double_library is new
> Ada.Numerics.Generic_Elementary_Functions(Interfaces.C.double);
> package double_output_library is new
> Ada.Text_IO.Float_IO(Interfaces.C.double);
> begin
>
> for I in 1 .. 1_000_000 loop --I would not have this loop but one
> compiler crashed if
> --the source file was sized a few
> megabytes.
>
> answer := Interfaces.C."+"(answer,
> double_library.log(0.10000000000000000000
> , 10.0 ));
> --Lines produced by
> --for((i=1; i<=500; ++i)) do echo -n 'answer := Interfaces.C."+"(answer,
> double_library.log(0' ; echo "$i/10" | bc -l ; echo ', 10.0 ));'; done
> answer := Interfaces.C."+"(answer,
> double_library.log(050.00000000000000000000
> , 10.0 ));
>
>
> end loop;
>
> double_output_library.Put(answer);
> end;
IIUC, your Ada program is equivalent to
with Ada.Numerics.Generic_Elementary_Functions;
with Ada.Text_IO;
with Interfaces.C;
procedure Optimization_Test is
subtype Double is Interfaces.C.Double;
use type Double;
package Math is new Ada.Numerics.Generic_Elementary_Functions (Double);
Answer : Double := 0.0;
begin -- Optimization_Test
for I in 1 .. 1_000_000 loop
Answer := Answer + Math.Log (0.1, 10.0);
for J in 1 .. 500 loop
Answer := Answer + Math.Log (Double (J) / 10.0, 10.0);
end loop;
Answer := Answer + Math.Log (50.0, 10.0);
end loop;
Ada.Text_IO.Put (Answer'Img);
end Optimization_Test;
On my machine I get something like
jrcarter@jrcarter-acer-laptop:~/Code$ gnatmake -O2 -gnatnp optimization_test.adb
gcc-4.3 -c -O2 -gnatnp optimization_test.adb
gnatbind -x optimization_test.ali
gnatlink optimization_test.ali
jrcarter@jrcarter-acer-laptop:~/Code$ time ./optimization_test
6.34785378608078E+08
real 1m33.817s
user 1m33.810s
sys 0m0.000s
Note that suppressing runtime checks (-gnatp) is needed to be sort of equivalent
to C++.
A good compiler could recognize that this adds a constant amount to Answer each
time through the outer loop, and the outer loop is run a static constant number
of times, and might optimize it to something like
with Ada.Numerics.Generic_Elementary_Functions;
with Ada.Text_IO;
with Interfaces.C;
procedure Optimization_Test is
subtype Double is Interfaces.C.Double;
use type Double;
package Math is new Ada.Numerics.Generic_Elementary_Functions (Double);
function Inner_Loop return Double is
Result : Double := 0.0;
begin -- Inner_Loop
for J in 1 .. 500 loop
Result := Result + Math.Log (Double (J) / 10.0, 10.0);
end loop;
return Result;
end Inner_Loop;
pragma Inline (Inner_Loop);
Answer : constant Double :=
1.0E6 * (Math.Log (0.1, 10.0) + Inner_Loop + Math.Log (50.0, 10.0) );
begin -- Optimization_Test
Ada.Text_IO.Put (Answer'Img);
end Optimization_Test;
This, on my machine and with the same options, gives something like
jrcarter@jrcarter-acer-laptop:~/Code$ time ./optimization_test 6.34785378539470E+08
real 0m0.009s
user 0m0.000s
sys 0m0.010s
I suspect this optimization is what the C++ compiler is doing, and the Ada
compilers are not.
Whether a similar optimization may be applied to the calculations in your real
program of interest is another question.
--
Jeff Carter
"Friends don't let friends program in C++."
Ludovic Brenta
114
next prev parent reply other threads:[~2010-02-15 23:04 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-02-15 10:58 Is there an Ada compiler whose Ada.Numerics.Generic_Elementary_Functions.Log(Base=>10, X=>variable) is efficient? Colin Paul Gloster
2010-02-15 13:02 ` John B. Matthews
2010-02-15 14:17 ` Colin Paul Gloster
2010-02-15 17:19 ` John B. Matthews
2010-02-15 14:54 ` jonathan
2010-02-15 15:04 ` jonathan
2010-02-15 19:50 ` sjw
2010-02-16 16:50 ` Colin Paul Gloster
2010-02-15 18:26 ` (see below)
2010-02-15 18:51 ` jonathan
2010-02-15 20:00 ` sjw
2010-02-15 21:17 ` jonathan
2010-02-16 0:09 ` jonathan
2010-02-16 17:33 ` Colin Paul Gloster
2010-02-24 10:07 ` Colin Paul Gloster
2010-02-15 23:04 ` Jeffrey R. Carter [this message]
2010-02-16 14:54 ` Colin Paul Gloster
2010-02-16 15:24 ` Colin Paul Gloster
2010-02-16 19:01 ` Jeffrey R. Carter
2010-02-17 10:25 ` Colin Paul Gloster
2010-02-15 23:20 ` Randy Brukardt
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox