home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 18902 < prev    next >
Encoding:
Text File  |  1992-12-26  |  2.1 KB  |  52 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!sal.wisc.edu!alan
  3. From: alan@sal.wisc.edu (Alan Watson)
  4. Subject: Re: speed?  double vs. float.  math.h??
  5. Message-ID: <1992Dec26.174403.17913@sal.wisc.edu>
  6. Organization: Space Astronomy Lab, Madison WI
  7. References: <1hh801INNab1@agate.berkeley.edu>
  8. Date: Sat, 26 Dec 1992 17:44:03 GMT
  9. Lines: 41
  10.  
  11. In article <1hh801INNab1@agate.berkeley.edu> c60b-3hu@web-3c.berkeley.edu (Paul Hsu) writes:
  12. >
  13. >Trying to make the program run fast, I would like to have only float 
  14. >variables, instead of doubles, in my program.  But in the standard math 
  15. >library, all the functions take in arguments and return values in the type 
  16. >double.  So, what should I do?
  17.  
  18. Your saving depends on your compiler and architecture.  I have only
  19. noticed order 20% speed increases when moving from doubles to floats in
  20. register-intensive code on RISC machines and VAXes, but on other machines
  21. (without a FPU, perhaps, or using H_FLOATs on uVAXes :-) there may be
  22. more of a problem.  I'd suggest benchmarking a test program to help you
  23. decide if any effort is worthwhile.
  24.  
  25. However, if you are using standard mathematic functions extensively,
  26. then your overhead will increase, because of those extra 7 digits or so
  27. of precision which get calculated.  See if your machine has a
  28. (non-standard) single precision library (sinf, cosf, etc.).  Or write
  29. one yourself.  I have found that one can squeeze perhaps 30% from the
  30. double routines by dropping to floats, using floating-point format
  31. dependent but otherwise standard C.  Writing your own math library is a
  32. very good educational experience for a numerical programmer -- it lets
  33. you know all about those different kinds of errors.  (It actually is
  34. fairly easy, once you get going -- writing a good double precision
  35. library is a job for professional, though.)
  36.  
  37. Using casts will not help at all -- you compiler is already putting them
  38. in implicitly, i.e., it is re-writing:
  39.  
  40.     #include <math.h>
  41.     float x, y = 1.0;
  42.     x = sin (y);
  43.  
  44. as:
  45.  
  46.     #include <math.h>
  47.     float x, y = (float) 1.0;
  48.     x = (float) sin ((double) y);
  49.  
  50. Is it worth all the effort to get an extra 30% from the code?  Only you
  51. can decide.
  52.