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

atul k chhabra achhabra at uceng.UC.EDU
Wed Mar 8 16:35:04 AEST 1989


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)
(of course, on Sun4 the -f68881 flag was not used.) The results are similar
on both machines. Can anyone enlighten me on this bizzare result?

Listing of cosc.c:
--------------------------------------------------------------------------------
/*
 * Compile using:
 *	cc -f68881 -O -o cosc cosc.c -lm.
 */

#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);
}
--------------------------------------------------------------------------------

Listing of cosf.f
--------------------------------------------------------------------------------
c
c	Compile using:
c		f77 -f68881 -O -o cosf cosf.f
c
	program cosf
	integer i
	real tmp

	do 10 i=1,262144
		tmp=cos(2.5)*cos(2.5)*cos(2.5)*cos(2.5)
10	continue
	end
--------------------------------------------------------------------------------

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
^^^^
--------------------------------------------------------------------------------

===========================================================================
Atul Chhabra, Dept. of Electrical & Computer Engineering, ML 030,
University of Cincinnati, Cincinnati, OH 45221-0030.

voice: (513)556-4766  INTERNET: achhabra at ucesp1.ece.uc.edu
                                OR achhabra at uceng.uc.edu
===========================================================================



More information about the Comp.lang.c mailing list