Fortran computes cosine 300 times faster than C (on Sun3)

Robert Forsman thoth at beach.cis.ufl.edu
Thu Mar 9 12:29:33 AEST 1989


>From: achhabra at uceng.UC.EDU (atul k chhabra)

>I chanced upon a segment of code that runs approximately 300 times
>faster in FORTRAN than in C. I have tried the code on Sun3(OS3.5) and
>on Sun4(OS4.0) (of course, on Sun4 the -f68881 flag was not used.)
>The results are similar on both machines. Can anyone enlighten me on
>this bizarre result?

>    for(i=0;i<262144;i++)
>	tmp=cos(2.5)*cos(2.5)*cos(2.5)*cos(2.5);

>	[equivalent FORTRASH code omitted]

Simple.  Fortran compilers usually optimize code to death.  From
reading the postings of others on this subject I figure it can do one
of several drastic things.
  Most drastic	- skip the computation;  the result is never used.
  #2		- say	tmp=cos(2.5)**4, that's all that happens
			anyway.

There are probably others but I should think that your average
knowledgeable FORTRAN programmer would spit on anything that did less
than number 2.  A smart C compiler could come close but you would have
to flip a few switches.
	From what I've heard, FORTRAN compilers have been ludicrously
optimizing since the dawn of time (~1950?) and as such are the
language of choice for supercomputers and other number crunchers.  I
would much rather use C but I can't remember any huge interest in
optimizing C code to death.  Just think what it would do to your
timing loops 
  for (i=0; i<6 jillion; i++) {}
optimized into nothing.

---------------------------------------------------------------------
Just say maybe to .signatures



More information about the Comp.lang.c mailing list