comp.lang.ada
 help / color / mirror / Atom feed
* GNAT Optimization of Constant Expressions
@ 2007-05-16 22:37 David Smith
  2007-05-17  4:50 ` Randy Brukardt
                   ` (2 more replies)
  0 siblings, 3 replies; 24+ messages in thread
From: David Smith @ 2007-05-16 22:37 UTC (permalink / raw)


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;




^ permalink raw reply	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2007-05-19  2:44 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-05-16 22:37 GNAT Optimization of Constant Expressions David Smith
2007-05-17  4:50 ` Randy Brukardt
2007-05-17 20:03   ` Gautier
2007-05-17 20:46     ` Randy Brukardt
2007-05-17 22:49       ` David Smith
2007-05-17 23:38         ` Randy Brukardt
2007-05-18  7:58           ` Dmitry A. Kazakov
2007-05-18 11:27           ` Georg Bauhaus
2007-05-18 17:28             ` Randy Brukardt
2007-05-18 16:25       ` Ray Blaak
2007-05-18 17:40         ` Randy Brukardt
2007-05-18 22:51           ` Adam Beneschan
2007-05-19  2:44             ` Randy Brukardt
2007-05-18  9:05   ` Markus E Leypold
2007-05-18  9:47   ` Florian Weimer
2007-05-18 11:32     ` Duncan Sands
2007-05-18 17:20     ` Randy Brukardt
2007-05-18 17:35       ` Duncan Sands
     [not found]       ` <200705181935.23877.baldrick@free.fr>
2007-05-18 17:49         ` Duncan Sands
2007-05-17  5:30 ` Martin Krischik
2007-05-18  9:56 ` Duncan Sands
2007-05-18 15:39   ` David Smith
2007-05-18 17:08     ` Duncan Sands
     [not found]     ` <200705181908.54920.baldrick@free.fr>
2007-05-18 17:32       ` Duncan Sands

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox