🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.sun
Article #12027

Re: Fortran 300 times faster than C for computing cosine

#12027
From: attcan!utzoo!henry
Date: Tue, 18 Apr 1989 18:38
21 lines
1517 bytes
>	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@zoo.toronto.edu
Technical Details
Message-ID: <8903230410.AA29419@uunet.UU.NET>
Path: arc1-ca.pugleaf.net!utzoo!dptcdc!jarvis.csri.toronto.edu!mailrus!cornell!uw-beaver!rice!sun-spots-request

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!dptcdc!jarvis.csri.toronto.edu!mailrus!cornell!uw-beaver!rice!sun-spots-request
From: attcan!utzoo!henry@uunet.uu.net
Newsgroups: comp.sys.sun
Subject: Re: Fortran 300 times faster than C for computing cosine
Keywords: Software
Message-ID: <8903230410.AA29419@uunet.UU.NET>
Date: 18 Apr 89 18:38:07 GMT
Sender: usenet@rice.edu
Organization: Sun-Spots
Lines: 21
Approved: Sun-Spots@rice.edu
Original-Date: Wed, 22 Mar 89 23:10:10 -0500
X-Sun-Spots-Digest: Volume 7, Issue 230, message 2 of 16