Thread: Float vs Double for Speed?

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    106

    Float vs Double for Speed?

    Hi Guys,
    Is it true that instructions execute faster in modern architecture if
    the variables used for trig functions are doubles instead of floats?

    And if so, why would that be?
    Thanks

  2. #2
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Consider sin; it accepts an argument of the type double and returns a value of the type double, so if you pass a float and assign to a float, that's two type conversions your C implementation needs to perform. For most cases I'd say the time it takes to perform those conversions is negligible, but you can try using sinf if you find that calls to sin are slow.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Not true in general, regardless of whether an architecture is modern or not. It can be true, depending on the hardware design, and what it is optimised to achieve.

    There are many factors other than performance of single instructions (multi-tiered memory architecture, instruction pipelining, speculative execution) that affect all types of system performance, including numerical performance.

    The Cray-1 (a supercomputer delivered in the late 1970s) had native hardware support for double precision operations, and single-precision operations were emulated in software (therefore slower, despite consuming less memory).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    106
    Is there any problem that can result from changing every float declared in a program, and replacing it with doubles?
    Assuming you haven't done anything fancy that accesses their bytes, etc.
    Some of my program is (I think) Commodore 64 code which used only floats.

  5. #5
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Why would you want to do that? If you're concerned about the performance of your code I suggest using a profiler like valgrind or gprof.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    As far as C goes, assuming that's what you mean, 'double' is the default data type that's passed to math functions and printf, etc. So as was stated before, using 'float' saves only storage space but needs on-the-fly conversion to double when it's used.
    What's actually faster in hardware is using 80-bit floats, which is native to the floating-point hardware internally. A hold-over from the co-processor days. However, C compilers do not generally support that data type for the programmer.

    I don't see any problem changing every occurrence of 'float' to 'double' while porting code from Commodore. Be sure to check any formatting strings for input functions such as sscanf(), etc., so use %lf instead of %f to correctly interpret the value.

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    106
    Thanks, I have ported the code long ago, but was just wondering if there's any nasties
    to changing the variable types I might not know about. Doesn't hurt to try, but I don't
    want anything going wrong with a distribution release.
    You know you're getting desperate for optimisation when you sink to this!

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by xArt View Post
    You know you're getting desperate for optimisation when you sink to this!
    I've sunk there. Done that!

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by xArt View Post
    Is there any problem that can result from changing every float declared in a program, and replacing it with doubles?
    Assuming you haven't done anything fancy that accesses their bytes, etc.
    Some of my program is (I think) Commodore 64 code which used only floats.
    You shouldn't expect the results to match exactly, for one thing.

    Whether double or float is faster is a complicated issue. Such factors as rounding mode, space taken in cache, hardware support, and the level of strictness at which the compiler enforcing floating point rules can all come in to play. When fooling with this stuff in a shipping product, you had better have an extensive and trustworthy battery of tests.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Last I checked, in my software 3D engine, I found floats to still be marginally faster, but they did cause minor glitches due to the lesser precision in my case.
    Note, I have not tried 64-bit code as yet.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by nonoob View Post
    What's actually faster in hardware is using 80-bit floats, which is native to the floating-point hardware internally.
    On certain x86 hardware and OS combinations, maybe. Even on systems using x87 code, it could still depend on whether the code is FPU bound or memory bound. And even taking that into account, using 80 bit values instead of doubles could prevent the compiler from doing various optimizations.

    As others have said, if it's running slow, profile your code. Don't just guess.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 06-20-2012, 02:34 AM
  2. float vs Double
    By sughandh in forum C Programming
    Replies: 3
    Last Post: 07-16-2006, 04:26 AM
  3. float; double; int
    By Lyanette in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2003, 12:04 PM
  4. Float,double,int
    By Bill 101 in forum C Programming
    Replies: 1
    Last Post: 11-06-2002, 02:09 PM