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 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,13280cdb905844e4 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!news3.google.com!feeder.news-service.com!weretis.net!feeder2.news.weretis.net!news.tornevall.net!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada 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 Organization: TornevallNET - http://news.tornevall.net Message-ID: References: NNTP-Posting-Host: b2ba4fc03bb3c09ea63dee9e8744fd7e Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: e0d2cef6e1495b417cc7aefdfa816524 X-Complaints-To: abuse@tornevall.net X-Complaints-Language: Spoken language is english or swedish - NOT ITALIAN, FRENCH, GERMAN OR ANY OTHER LANGUAGE! In-Reply-To: X-Validate-Post: http://news.tornevall.net/validate.php?trace=e0d2cef6e1495b417cc7aefdfa816524 X-SpeedUI: 1738 X-Complaints-Italiano: Parlo la lingua non � italiano User-Agent: Thunderbird 2.0.0.23 (X11/20090817) X-Posting-User: 0243687135df8c4b260dd4a9a93c79bd Xref: g2news1.google.com comp.lang.ada:9257 Date: 2010-02-15T16:04:34-07:00 List-Id: 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