Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!purdue!decwrl!labrea!eos!ames!ll-xn!adelie!mirror!frog!john From: john@frog.UUCP (John Woods) Newsgroups: comp.lang.c Subject: Re: Fortran computes cosine 300 times faster than C (on Sun3) Message-ID: <1084@frog.UUCP> Date: 10 Mar 89 09:54:00 GMT References: <765@uceng.UC.EDU> Distribution: na Organization: Misanthropes-R-Us Lines: 41 In article , thoth@beach.cis.ufl.edu (Robert Forsman) writes: > >From: achhabra@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@frog.UUCP, ...!mit-eddie!jfw, jfw@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