Fortran 300 times faster than C for computing cosine

attcan!utzoo!henry at uunet.uu.net attcan!utzoo!henry at uunet.uu.net
Wed Apr 19 04:38:07 AEST 1989


>	do 10 i=1,262144
>		tmp=cos(2.5)*cos(2.5)*cos(2.5)*cos(2.5)

This one got hashed out at some length in comp.lang.c a while ago.  The
major reason for the difference in speed is as our moderator mentioned:
the Fortran compiler knows that cos() is a builtin function, notices that
the value is never used, and optimizes out the body of the loop.  C
compilers *could not* do this until recently, since it's only the
(imminent) ANSI C standard that has declared certain library functions to
be potentially built-in.

>... Add "print *,tmp" after the body of
>the loop and cosf.f takes alot longer.  Interestingly enough, it still
>appears to be quicker than cosc.c.  --wnl ]]

It may only be computing cos(2.5) once per loop, since the compiler is
entitled to notice the duplication.  Also, note that the Fortran version
is using single-precision arithmetic where the C one is using double.

	Henry Spencer at U of Toronto Zoology
	uunet!attcan!utzoo!henry henry at zoo.toronto.edu



More information about the Comp.sys.sun mailing list