comp.lang.ada
 help / color / mirror / Atom feed
From: " "@deneb.cygnus.argh.org (Florian Weimer)
Subject: Re: Gnat optimizes better than gcc C?
Date: 1999/12/22
Date: 1999-12-22T08:51:26+00:00	[thread overview]
Message-ID: <87so0v8ik2.fsf@deneb.cygnus.argh.org> (raw)
In-Reply-To: 83djjr$qqo$1@nnrp1.deja.com

Ted Dennison <dennison@telepath.com> writes:

> Hmm. Section 7.4 shows an example of an algorithm coded in Ada and C
> where the optimizer can make the Ada code run twice as fast as the C. 

The code is:

procedure bench is
--Simple benchmark program to test optimization
pragma optimize( time );
type bench_integer is new long_integer range long_integer'range;
type small_integer is new long_integer range 0..9;
function p( param : bench_integer ) return bench_integer is
  divideby : constant bench_integer := 4;
begin
  return param / divideby;
end p;
pragma inline( p );
j : bench_integer := bench_integer'last;
-- deliberate error in main program for j * 2
type atype is array(0..9) of small_integer;
--pragma pack( atype );
a : atype;
begin
  for i in 1..100_000_000 loop
  j := abs( p( bench_integer( i ) ) - (j * 2) );
  a( integer( j mod 10 ) ) := small_integer( j mod
    bench_integer( small_integer'last ) );
 end loop;
end bench;

A strange thing happens here: if (and only if) the pragma Pack is
activated, GNAT optimizes away most of the statements in the for loop.
(I guess because with pragma Pack, "atype" is treated as a 40-bit part
of a 64-bit integer internally , not as an array.)  This is permitted,
because the results of this calculations are never used.  But if you use
"a" after the for loop, this kind of optimization can no longer take
place, and the code doesn't run twice as fast, but three times slower
than the Ada code without pragma Pack (and, of course, the corresponding
C code).

I guess the C code runs faster on SteveD's machine because he used
another GCC backend to compile it.  When using the same 2.8.1 as GNAT,
the execution times are almost identical.




  parent reply	other threads:[~1999-12-22  0:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-12-15  0:00 GNAT use psimdars
1999-12-16  0:00 ` DuckE
1999-12-17  0:00   ` Gnat optimizes better than gcc C? Ted Dennison
1999-12-17  0:00     ` Preben Randhol
1999-12-22  0:00     ` Florian Weimer [this message]
1999-12-17  0:00 ` GNAT use Preben Randhol
replies disabled

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