Charles said:
I'm writing some real-time numerical code which needs to be pretty
nippy. Should I be using float or doubles for my calculations? I
know my P4 is a 32bit processor, but I don't know if the JVM will end
up putting the 32bits of the floats into the 32bits of my processor,
or if it does something else. In C the 64double would take much
longer to run on a 32bit processor -- is this still the case with the
virtual machine?
The best advice about optimization is almost invariably to test the
performance first, then adjust as necessary.
Floating-point math on x86 is performed in a FPU that uses 80-bit
operands and creates 80-bit results. This is true whether you start
with a 32-bit or a 64-bit value. In general, depending on whether
intermediate results can be held in the FPU instead of transferred back
to memory, you might find that there is no significant difference in
speed between 32-bit and 64-bit operands. If you do have to transfer
intermediate results then it takes longer to transfer a 64-bit value
than it does to transfer a 32-bit value on a 32-bit machine.
There are other factors as well. For instance, you can hold twice as
many 32-bit values in the CPU's cache; if your application holds a lot
of data or intermediate values in memory then this could be a
consideration. Of course, if the code is not structured so as to be
able to make efficient use of the cache then that in itself will
probably cause a bigger performance impact than 32-bitness vs. 64-bitness.
Don't take these for an exhaustive enumeration of the issues -- you can
find much more comprehensive analyses. But don't overlook my very first
comment: if your code is not running fast enough then you need to
determine _why_ by observation and measurement.
John Bollinger
(e-mail address removed)