Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!agate!eos!ames!killer!ernest!friday!fritz From: fritz@friday.UUCP (Fritz Whittington) Newsgroups: comp.lang.c Subject: Re: Fortran computes cosine 300 times faster than C (on Sun3) Keywords: Fortran, C, cosine, speed Message-ID: <688@friday.UUCP> Date: 9 Mar 89 20:42:32 GMT References: <765@uceng.UC.EDU> Reply-To: fritz@friday.UUCP (Fritz Whittington) Distribution: na Organization: Texas Instruments, Dallas, Texas Lines: 27 In article <765@uceng.UC.EDU> achhabra@uceng.UC.EDU (atul k chhabra) writes: >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) . . . > for(i=0;i<262144;i++) > tmp=cos(2.5)*cos(2.5)*cos(2.5)*cos(2.5); . . . >% time cosc >55.6u 1.0s 1:49 51% 24+8k 12+1io 0pf+0w >^^^^^ >% time cosf >0.2u 0.0s 0:00 75% 16+8k 4+0io 0pf+0w >^^^^ I suspect that the FORTRAN math library has been "memoized" and the C library hasn't. Memoization consists of having a function keep track of prior input-output pairs (at least the one from the previous call, sometimes a small hash table of prior calls); if called again with an input that matches one in its past history, it doesn't have to re-compute the output, simply supply it. You are calling with the same value all the time.... Try replacing the 2.5 with something like (i mod 5000) in both versions and compare again. ---- Fritz Whittington Texas Instruments, Incorporated I don't even claim these opinions myself! MS 3105 UUCP: killer!ernest!friday!fritz 8505 Forest Lane AT&T: (214)480-6302 Dallas, Texas 75243