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