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

John Woods john at frog.UUCP
Fri Mar 10 19:54:00 AEST 1989


In article <THOTH.89Mar8212933 at beach.cis.ufl.edu>, thoth at beach.cis.ufl.edu (Robert Forsman) writes:
> >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.
> >    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.
> 
    #3			tmp = 0.4119472		(since COS is hardwired into
						FORTRAN and the compiler can
						evaluate the constant
						expression itself.

> 	From what I've heard, FORTRAN compilers have been ludicrously
> optimizing since the dawn of time

An interesting story:  when I worked at Lincoln Labs, one group was buying
a VAX and wondered whether to run VMS or UNIX.  One person there was selected
to run a FORTRAN program that they were interested in through the VMS FORTRAN
and f77 compilers, and got the rather expected result that VMS FORTRAN created
a faster program (I think by 20% on that particular program).  But the
interesting part is this:  he also recoded the program in C, using tricks common
to C programmers but not doing any constant expression precalculation, and
came up with a program that ran twice as fast as the VMS version.

There's a lot to be said for highly optimizing compilers (ask any supercomputer
jock), but sometimes a Neanderthal language can get in the way of a clear (and
efficient) exposition of one's intent.

-- 
John Woods, Charles River Data Systems, Framingham MA, (508) 626-1101
...!decvax!frog!john, john at frog.UUCP, ...!mit-eddie!jfw, jfw at eddie.mit.edu

"He should be put in stocks in Lafeyette Square across from the White House
 and pelted with dead cats."	- George F. Will



More information about the Comp.lang.c mailing list