Alex Rivera | Logout

double or float, which is faster?

Asked 2011-01-03T13:02:29.237
71

I am reading "accelerated C++". I found one sentence which states "sometimes double is faster in execution than float in C++". After reading sentence I got confused about float and double working. Please explain this point to me.

Edit
Report

1 Answer

8

I can think of two basic cases when doubles are faster than floats:

  1. Your hardware supports double operations but not float operations, so floats will be emulated by software and therefore be slower.

  2. You really need the precision of doubles. Now, if you use floats anyway you will have to use two floats to reach similar precision to double. The emulation of a true double with floats will be slower than using floats in the first place.

    1. You do not necessarily need doubles but your numeric algorithm converges faster due to the enhanced precision of doubles. Also, doubles might offer enough precision to use a faster but numerically less stable algorithm at all.

For completeness' sake I also give some reasons for the opposite case of floats being faster. You can see for yourself whichs reasons dominate in your case:

  1. Floats are faster than doubles when you don't need double's precision and you are memory-bandwidth bound and your hardware doesn't carry a penalty on floats.

  2. They conserve memory-bandwidth because they occupy half the space per number.

  3. There are also platforms that can process more floats than doubles in parallel.

answered 2011-01-03T13:06:49.200

Your Answer