Fortran 300 times faster than C for computing cosine (on Sun3 & 4)

Rob McMahon mcvax!cu.warwick.ac.uk!cudcv at uunet.uu.net
Sat Apr 22 17:48:13 AEST 1989


In article <764 at uceng.UC.EDU> you write:
>I chanced upon a segment of code that runs approximately 300 times faster
>in FORTRAN than in C ...
>
>Timings on Sun3(OS3.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
>^^^^
>
>[[ ... [valid comments about results being optimised away when not used,
>because of the higher level of optimisation used by f77] ... --wnl ]]

The compiler might also be realising that cos returns the same value for
the same argument.  Here's a script using gcc, given the following
definition of cos, run on a Sun3/160 SunOS 4.0, and using gcc 1.34:

inline static const double
cos(double x)
{
    double result;
    asm("fcosx %1,%0" : "=f" (result) : "f" (x));
    return (result);
}

Script started on Wed Mar 29 22:06:38 1989
cudcv (6) >> cat x.c
#include <math.h>

main()
{
    int i;
    float tmp;

    for(i=0;i<262144;i++)
        tmp=cos(2.5)*cos(2.5)*cos(2.5)*cos(2.5);
}
cudcv (7) >> gcc -O x.c
cudcv (8) >> time ./a.out
0.2u 0.0s 0:00 141% 0+8k 0+0io 0pf+0w
cudcv (9) >> ed x.c
126
/}/i
    printf("%f\n", tmp);



More information about the Comp.sys.sun mailing list