home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / c / 13197 < prev    next >
Encoding:
Internet Message Format  |  1992-09-03  |  2.5 KB

  1. Path: sparky!uunet!dtix!darwin.sura.net!uvaarpa!cv3.cv.nrao.edu!laphroaig!cflatter
  2. From: cflatter@nrao.edu (Chris Flatters)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: C vs. FORTRAN
  5. Message-ID: <1992Sep3.225136.27006@nrao.edu>
  6. Date: 3 Sep 92 22:51:36 GMT
  7. References: <DWISEMAN.92Sep3164036@spsd630a.erim.org>
  8. Sender: news@nrao.edu
  9. Reply-To: cflatter@nrao.edu
  10. Organization: NRAO
  11. Lines: 68
  12.  
  13. In article 92Sep3164036@spsd630a.erim.org, dwiseman@spsd630a.erim.org (Dave Wiseman) writes:
  14. >
  15. >Help!
  16. >
  17. >I have a (two) simple programs, one in fortran, and one in C.  I have compiled
  18. >them both optimized, and timed the results.  The fortran version takes .1 s
  19. >to run, and the C version takes 12.5s to run.  I know (hope) that there is a
  20. >simple remedy for this.  The project that I am working on has to do many 
  21. >sines and cosines, and I'd prefer to keep the entire program in C, and not
  22. >mix the languages.  What can I do to improve the speed of the C version?
  23. >
  24. >    Dave Wiseman
  25. >    dwiseman@erim.org
  26. >
  27. >-------------------------------------------------------------------------------
  28. >#include <stdio.h>
  29. >#include <math.h>
  30. >
  31. >main (argc,argv)
  32. >     int argc;
  33. >     char **argv;
  34. >{
  35. >  double angle,delta,the_sin,the_cos;
  36. >  int i;
  37. >  
  38. >  angle = 0.0;
  39. >  delta = 0.01;
  40. >  
  41. >  for (i=0; i<1000000; i++)
  42. >  {
  43. >    the_sin = sin(angle);
  44. >    the_cos = cos(angle);
  45. >    angle += delta; 
  46. >  }
  47. >}
  48. >-------------------------------------------------------------------------------
  49. >      program test
  50. >
  51. >      real*8 angle,delta,the_sin,the_cos
  52. >      integer*4 i
  53. >
  54. >      angle = 0.0
  55. >      delta = 0.01
  56. >
  57. >      do I=1,1000000
  58. >         the_sin = dsin(angle)
  59. >         the_cos = dcos(angle)
  60. >         angle = angle + delta
  61. >      enddo
  62. >
  63. >      end
  64.  
  65. Beware of timing trivial programs.  Since the first 999 999 values of the_sin
  66. and the_cos are discarded an optimised is at liberty to avoid computing them.
  67. It is likely that the FORTRAN compiler did this and while the C compiler
  68. did not.  I am fairly sure that if the_sin and the_cos were made into arrays
  69. and if the sin and cos results were stored into the_sin(i) and the_cos(i)
  70. on every loop the C and FORTRAN versions would take almost exactly the
  71. same time to run.
  72.  
  73. The only other likely possibility is that the FORTRAN compiler is exploiting
  74. a hardware implementation of the transcendental functions while the C compiler
  75. is not.  This is only a significant possibility on Intel 80x86/80x87 machines
  76. and since the 0.1 second time for the FORTRAN implies a floating-point
  77. performance in excess of 30 MFLOPS it is a very remote one.
  78.  
  79.     Chris Flatters
  80.     cflatter@nrao.edu
  81.