Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!uw-beaver!rice!sun-spots-request From: mcvax!cu.warwick.ac.uk!cudcv@uunet.uu.net (Rob McMahon) Newsgroups: comp.sys.sun Subject: Re: Fortran 300 times faster than C for computing cosine (on Sun3 & 4) Keywords: Software Message-ID: <135@titania.warwick.ac.uk> Date: 22 Apr 89 07:48:13 GMT References: <764@uceng.UC.EDU> Sender: usenet@rice.edu Organization: Computing Services, Warwick University, UK Lines: 47 Approved: Sun-Spots@rice.edu Original-Date: 29 Mar 89 21:19:02 GMT X-Sun-Spots-Digest: Volume 7, Issue 239, message 3 of 21 In article <764@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 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);