Fog Creek Software
Discussion Board

a bit offtopic - float vs. double performance?

hello, i have looked around and could not find an answer to my question.

when coding in c/c++, will using float variables give an increase in performance? will doubles? which usually run faster?

is the difference between the two basically negligible?


Warren Henning
Thursday, December 6, 2001

Why don't you experiment and see?

Thursday, December 6, 2001

That's a highly technical discussion, but to cut several corners: the speed difference is negligible for 99% of all applications.

Jan Derk

Jan Derk
Thursday, December 6, 2001

I've seen too many people being proud of gaining 5% speed in a rarely used method by doing some time consuming floating point math optimizations, while at the same time they were reading data from disk instead of memory in an inner loop.

In addition, I've seen those same people on a long time wasting hunt for a bug that was caused by the limited precision of a single float which would have never occurred if they had used doubles in the first place.

Jan Derk
Thursday, December 6, 2001

Start with doubles because they've got more precision. On x86 the FPU treats floats & doubles the same internally, so there is no speed difference (aside from the extra memory bandwidth for double loads/stores and larger cache footprint). If profiling suggests that memory bandwidth is a problem, consider switching to floats.

Dan Maas
Thursday, December 6, 2001

What about fixed point integer math ?

Philippe Back
Thursday, December 6, 2001

To answer the original question floats are usually 2 to 4 times faster than doubles.

Using a 100,000,000 iteration loop an empty loop completes in ~0.5 s. One with basic float operations(*,/,-,+) in ~2.1 s and the same functions with double in 6.0 s. The differance between double and float is therefore  5.5/1.6 which is approx 3.43. (Using gcc on RedHat/Linux).

Note that the other comments in this article are still relevant. Focussing on float vs double performance is largely irrelevant as most time in a program is usally spent in disk/memory access. It is usually better to accept the performance hit to avoid precision problems.

Julian Bullock
Thursday, December 6, 2001

Julian: what architecture are you on? (I assume a Pentium-class system, but just checking).

Pentiums are strange beasts.  I have seen loops execute *faster* when you insert a few NOPs here and there.  True story.

I believe that the x86 FPU treats floats and doubles identically -- however, a single-precision constant load may be faster because it's loading four bytes from memory instead of eight.  Just a theory.

Anyways, you're right.  Most of the time the speed difference is negligible.  And it's different on every architecture.  Even between Intel and AMD chips.  If you really want to know much it'll impact your program, the best thing is to try it and time it.

Friday, December 7, 2001

Also did a small floating point test using Delphi 6:

Single (32 bits): 1.712 s
Real48 (48 bits): 2.003 s
Double (64 bits): 1.733 s
Extended (80 bits): 1.772 s

So the answers for Delphi/Windows2000/Athlon is using singles does not gain you anything significant in speed over doubles.

Jan Derk
Friday, December 7, 2001

Not all languages or implementations of languages actually make a distinction in the storing of floats or doubles, they tend to use the 80bits that the processor uses.  The distinctions they make are in the presentation of those numbers, which is actually more likely to give you rounding errors. 

Whichever size you use, use one don't mess about mixing sizes, don't depend on the runtime increasing the precision of the calculation and then rounding for you as in multiplying two large floats or one float and a double.

Simon Lucy
Friday, December 7, 2001

The answer to this question is platform dependant. Assuming you are working with a recent crop of the x86 processors, anything from Pentium II onwards. It really makes little difference. floats an integers are both treated similarly. If you realy are shooting for performance and do not care about precission, then you can tell the coprocessor to work in low-precission mode and use floats instead of doubles. A tiny chunk of assembler (that I don't seem to find right now ;) will do that for you.

Beka Pantone
Wednesday, December 26, 2001

*  Recent Topics

*  Fog Creek Home