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.5 required=5.0 tests=BAYES_00,FROMSPACE, FROM_ADDR_WS,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,dd5ebdd28e541cfa X-Google-Attributes: gid103376,public From: " "@deneb.cygnus.argh.org (Florian Weimer) Subject: Re: Gnat optimizes better than gcc C? Date: 1999/12/22 Message-ID: <87so0v8ik2.fsf@deneb.cygnus.argh.org>#1/1 X-Deja-AN: 563742091 References: <385800DC.DE033376@lisco.com> <3859a321.0@news.pacifier.com> <83djjr$qqo$1@nnrp1.deja.com> Mail-Copies-To: never Content-Type: text/plain; charset=us-ascii X-Complaints-To: abuse@cygnus.argh.org X-Trace: deneb.cygnus.argh.org 945852686 5878 192.168.1.2 (22 Dec 1999 08:51:26 GMT) Organization: Penguin on board User-Agent: Gnus/5.0803 (Gnus v5.8.3) Emacs/20.4 Mime-Version: 1.0 NNTP-Posting-Date: 22 Dec 1999 08:51:26 GMT Newsgroups: comp.lang.ada Date: 1999-12-22T08:51:26+00:00 List-Id: Ted Dennison 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.