home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!dtix!darwin.sura.net!uvaarpa!cv3.cv.nrao.edu!laphroaig!cflatter
- From: cflatter@nrao.edu (Chris Flatters)
- Newsgroups: comp.lang.c
- Subject: Re: C vs. FORTRAN
- Message-ID: <1992Sep3.225136.27006@nrao.edu>
- Date: 3 Sep 92 22:51:36 GMT
- References: <DWISEMAN.92Sep3164036@spsd630a.erim.org>
- Sender: news@nrao.edu
- Reply-To: cflatter@nrao.edu
- Organization: NRAO
- Lines: 68
-
- In article 92Sep3164036@spsd630a.erim.org, dwiseman@spsd630a.erim.org (Dave Wiseman) writes:
- >
- >Help!
- >
- >I have a (two) simple programs, one in fortran, and one in C. I have compiled
- >them both optimized, and timed the results. The fortran version takes .1 s
- >to run, and the C version takes 12.5s to run. I know (hope) that there is a
- >simple remedy for this. The project that I am working on has to do many
- >sines and cosines, and I'd prefer to keep the entire program in C, and not
- >mix the languages. What can I do to improve the speed of the C version?
- >
- > Dave Wiseman
- > dwiseman@erim.org
- >
- >-------------------------------------------------------------------------------
- >#include <stdio.h>
- >#include <math.h>
- >
- >main (argc,argv)
- > int argc;
- > char **argv;
- >{
- > double angle,delta,the_sin,the_cos;
- > int i;
- >
- > angle = 0.0;
- > delta = 0.01;
- >
- > for (i=0; i<1000000; i++)
- > {
- > the_sin = sin(angle);
- > the_cos = cos(angle);
- > angle += delta;
- > }
- >}
- >-------------------------------------------------------------------------------
- > program test
- >
- > real*8 angle,delta,the_sin,the_cos
- > integer*4 i
- >
- > angle = 0.0
- > delta = 0.01
- >
- > do I=1,1000000
- > the_sin = dsin(angle)
- > the_cos = dcos(angle)
- > angle = angle + delta
- > enddo
- >
- > end
-
- Beware of timing trivial programs. Since the first 999 999 values of the_sin
- and the_cos are discarded an optimised is at liberty to avoid computing them.
- It is likely that the FORTRAN compiler did this and while the C compiler
- did not. I am fairly sure that if the_sin and the_cos were made into arrays
- and if the sin and cos results were stored into the_sin(i) and the_cos(i)
- on every loop the C and FORTRAN versions would take almost exactly the
- same time to run.
-
- The only other likely possibility is that the FORTRAN compiler is exploiting
- a hardware implementation of the transcendental functions while the C compiler
- is not. This is only a significant possibility on Intel 80x86/80x87 machines
- and since the 0.1 second time for the FORTRAN implies a floating-point
- performance in excess of 30 MFLOPS it is a very remote one.
-
- Chris Flatters
- cflatter@nrao.edu
-