Skip to main content

Double vs Float performance

3 replies [Last post]
ted_graham
Offline
Joined: 2007-11-14

We have a trading application that does all its floating point calculations using doubles. While reading "Programming Pearls", it occurred to me that we don't need the extra precision of a double and that switching to floats should use less memory and might improve performance.

We aren't doing huge amounts of matrix math, there are probably only a couple hundred floating point operations in our processing of a typical message.

Anyone care to speculate on the performance impacts of this change?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
p_a_harvey
Offline
Joined: 2008-02-25

The difference between float and double is not that significant. Maybe 10% to 15% on a modern system. The following is the results of some internal testing where I work:

Nanoseconds : Test
367940 : Summing array of 10000 floats ten times
421753 : Summing array of 10000 doubles ten times
232473 : Summing array of 10000 ints ten times
377685 : Summing array of 10000 longs ten times
3575212 : Summing array of 100000 floats ten times
4262511 : Summing array of 100000 doubles ten times
2307389 : Summing array of 100000 ints ten times
3799013 : Summing array of 100000 longs ten times

These numbers are from a reasonably modern dual-core Linux system and a recent Sun Java release. Each number is the average time for 10,000 runs of each of the tests, discarding any significant outlier runs caused by GC, mouse movements, hotspot warmup, etc. Be aware that your performance results will vary by processor, platform, etc. We've found other processors where doubles were faster than longs, for example.

mthornton
Offline
Joined: 2003-06-10

Unless you are processing many thousands of messages per second I wouldn't bother. Sometimes float is worth using, not because the operations are much faster, but because it uses half the memory. Again don't bother unless you are using hundreds of megabytes or are otherwise unusually memory constrained.

ted_graham
Offline
Joined: 2007-11-14

We can see 1000 messages/sec, but we are primarily concerned with performance, not memory usage.