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

Fritz Whittington fritz at friday.UUCP
Fri Mar 10 06:42:32 AEST 1989


In article <765 at uceng.UC.EDU> achhabra at 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



More information about the Comp.lang.c mailing list