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=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,acba876b1e3c9639,start X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!q75g2000hsh.googlegroups.com!not-for-mail From: David Smith Newsgroups: comp.lang.ada Subject: GNAT Optimization of Constant Expressions Date: 16 May 2007 15:37:08 -0700 Organization: http://groups.google.com Message-ID: <1179355028.624745.258370@q75g2000hsh.googlegroups.com> NNTP-Posting-Host: 72.211.146.117 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1179355029 2318 127.0.0.1 (16 May 2007 22:37:09 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 16 May 2007 22:37:09 +0000 (UTC) User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.3) Gecko/20070321 Firefox/2.0.0.3 (Swiftfox),gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: q75g2000hsh.googlegroups.com; posting-host=72.211.146.117; posting-account=hjwdNg0AAADrWbU8BVPocB6GQuDF5upL Xref: g2news1.google.com comp.lang.ada:15818 Date: 2007-05-16T15:37:08-07:00 List-Id: In running some benchmarks between Ada, C, and Fortran, I came across a bizarre situation with GNAT. The same algorithm ran 10x slower in Ada, so I surmised that the constant math expressions in the code below were being precomputed by the compiler in C and Fortran, but not in Ada. To test my hypothesis, I defined the commented-out constants and replaced the math functions with the appropriate constant. This worked, and the Ada code was then as fast at the others. The bizarreness came out when I put the math expressions back in but left in the constant declarations. The code was still fast! I'm not even using the constants, but somehow they are helping the compiler optimize the code. In short, when I un-comment the constants, the code is super fast, but when commented out, the code is slow, even though I never use them. Does anyone know why this could be? I've looked through tons of documentation but haven't found anything like this. Listing: ------------------------------------------------------------------------------- with Ada.Text_IO; with Ada.Numerics.Generic_Elementary_Functions; procedure Bench is type Real is digits 15; type Real_Array is array(Integer range <>) of Real; package RMath is new Ada.Numerics.Generic_Elementary_Functions(Real); use RMath; N : constant Natural := 10_000; --SINX : constant Real := Sin(0.5); --SINY : constant Real := Sin(1.0); --SINZ : constant Real := Sin(1.5); --LOGX : constant Real := Log(0.5); --LOGY : constant Real := Log(1.0); --LOGZ : constant Real := Log(1.5); --SQRTX : constant Real := Sqrt(0.5); --SQRTY : constant Real := Sqrt(1.0); --SQRTZ : constant Real := Sqrt(1.5); X, Y, Z : Real_Array(1 .. N) := (others => 0.0); begin for J in 1 .. 10_000 loop for I in 1 .. N loop X(I) := X(I) + Real(J) + Sin(0.5)/1.5 + Log(0.5)/2.5 + Sqrt(0.5)/3.3; Y(I) := Y(I) + Real(J) + Log(1.0)/1.5 + Sqrt(1.0)/2.5 + Sin(1.0)/3.3; Z(I) := Z(I) + Real(J) + Sqrt(1.5)/1.5 + Sin(1.5)/2.5 + Log(1.5)/3.3; end loop; end loop; Ada.Text_IO.Put_Line(Real'Image(X(N)) & Real'Image(Y(N)) & Real'Image(Z(N))); end Bench;