Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uflorida!beach.cis.udl.edu!thoth From: thoth@beach.cis.ufl.edu (Robert Forsman) Newsgroups: comp.lang.c Subject: Re: Fortran computes cosine 300 times faster than C (on Sun3) Message-ID: Date: 9 Mar 89 02:29:33 GMT References: <765@uceng.UC.EDU> Sender: news@uflorida.cis.ufl.EDU Distribution: na Organization: University of Florida CIS Department Lines: 36 In-reply-to: achhabra@uceng.UC.EDU's message of 8 Mar 89 06:35:04 GMT >From: achhabra@uceng.UC.EDU (atul k chhabra) >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 bizarre result? > for(i=0;i<262144;i++) > tmp=cos(2.5)*cos(2.5)*cos(2.5)*cos(2.5); > [equivalent FORTRASH code omitted] Simple. Fortran compilers usually optimize code to death. From reading the postings of others on this subject I figure it can do one of several drastic things. Most drastic - skip the computation; the result is never used. #2 - say tmp=cos(2.5)**4, that's all that happens anyway. There are probably others but I should think that your average knowledgeable FORTRAN programmer would spit on anything that did less than number 2. A smart C compiler could come close but you would have to flip a few switches. From what I've heard, FORTRAN compilers have been ludicrously optimizing since the dawn of time (~1950?) and as such are the language of choice for supercomputers and other number crunchers. I would much rather use C but I can't remember any huge interest in optimizing C code to death. Just think what it would do to your timing loops for (i=0; i<6 jillion; i++) {} optimized into nothing. --------------------------------------------------------------------- Just say maybe to .signatures