delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2006/03/28/06:15:47

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
From: Hans-Bernhard Broeker <broeker AT physik DOT rwth-aachen DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: new gcc 4.10 behaviour
Date: 28 Mar 2006 11:11:38 GMT
Lines: 78
Message-ID: <48sjvaFlir4cU1@news.dfncis.de>
References: <1143542847 DOT 613686 DOT 87820 AT v46g2000cwv DOT googlegroups DOT com>
X-Trace: news.dfncis.de NZ5tQnyoKnjVu8+n/2i8XghSK77+wbE6l2iYuK/q92ksD8H5HazsV3sgD7
X-Orig-Path: not-for-mail
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Bob W <dontsend AT operamail DOT com> wrote:

> The following program was simplified as much as possible
> and it should be run with a command line parameter of
> 11 or 12 (based on a 2GHz P4) to verify the effects:
> ------------------

> #include <stdio.h>
> #include <stdlib.h>
> #include <time.h>

> int Ack(int m, int n) {
>   return(m ? (Ack(m-1,n ? Ack(m,(n-1)) : 1)) : n+1);
> }

> int main(int ac, char **av) {
>   int n = (ac == 2) ? atoi(av[1]) : 1;
>   clock_t t0=clock();
>   int ackret=Ack(3, n);
>   clock_t t1=clock();

>   printf("Ack(3,%d): %d\n", n, ackret);
>   printf("Time: %g secs\n", 1.0*(t1-t0)/CLOCKS_PER_SEC);
>   printf("t1:%d, t0:%d, cps:%d\n",t1,t0,CLOCKS_PER_SEC);

>   return 0;
> }

> ------------------

> Findings:
> - If compiled without optimisation the program is obviously slow,
>   but it seems to work as it did with gcc 4.01.
> - Every optimisation level from -O to -O3 gives various problems,
>   depending on the value of the command line parameter:
>   - Wrong value returned by clock().  - OR -

How do you know it's wrong?  I suspect your test program makes too
many unwarranted assumptions to actually deduce any expected
behaviour.  The key problem is that you're expecting variable
intializers to be executed strictly in sequence.  I don't think that
expectation is backed up by the language definition.

I.e. there's nothing in the language definition forbidding the compiler
to change 

>   clock_t t0=clock();
>   int ackret=Ack(3, n);
>   clock_t t1=clock();

>   printf("Ack(3,%d): %d\n", n, ackret);
>   printf("Time: %g secs\n", 1.0*(t1-t0)/CLOCKS_PER_SEC);

into

>   clock_t t0=clock();
>   clock_t t1=clock();
>   int ackret=Ack(3, n);

>   printf("Ack(3,%d): %d\n", n, ackret);
>   printf("Time: %g secs\n", 1.0*(t1-t0)/CLOCKS_PER_SEC);

or even

>   int ackret=Ack(3, n);
>   printf("Ack(3,%d): %d\n", n, ackret);
>   clock_t t0=clock();
>   clock_t t1=clock();
>   printf("Time: %g secs\n", 1.0*(t1-t0)/CLOCKS_PER_SEC);

And that's before we consider that it may have partially inlined Ack()
and intermingled it with the calls to clock() to speed up things even
more.  The optimizer is allowed to do that because Ack() is a local
function.
	
-- 
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019