Mail Archives: djgpp/2006/03/28/06:15:47
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 -