JAVA/C++: Performance of floating point calculations
How can i do floating point calculations in Java as quick
as in C/C++. In C/C++ the compiler can do very good optimizations on the programm. I thought that
multiplications are cheaper than divisions, so often i
just calculate the reciprocal of the number to be divided
with and use this value.
An other thing may be hardware issues. I made a sample
programme for Java and C++. Only thing i did is just
repeat a division for a couple of times. I always use
the same value, so there might happen hardware optimizations
as well. I runned the programmes on a AMD Athlon machine.
I can post programmes and result, if someone is interessted.
I think the problem is the JVM or the JITC. AFAIK your program can't force register calculations; it's up to the implementation on the user's machine. Just a guess, but because Java was designed for portable GUI (hence low on data processing) applications, they didn't spend a lot of time optimizing that sort of thing.
Yes, true. What i want to achieve is to normalize a set of values. I need to divide
every number by the average value. When i multiply every value with the reciprocal of
the average value, it may be cheaper than dividing everytime with the average value.
However it is not a very time consuming programme, so it shouldn't matter about a few milliseconds. Thanks for your answers.
For those interessted, here are my results. (Division took: is in milliseconds)(yes,
it was 1.000.000.000 calculations:)
This does not really fit my problem. I will only do about 50 ~ 100 calculations,
so there is no need to optimize.
JAVA (did this one three times:)
Division took: 9126 result: 31.84713375796178
Multiplication took: 6263 result: 31.84713375796178
Division took: 9630 result: 31.84713375796178
Multiplication took: 6627 result: 31.84713375796178
Division took: 8925 result: 31.84713375796178
Multiplication took: 6227 result: 31.84713375796178
C++ (build with gcc 3.4.3 no optimization)
jim@Yoper ~/prog/test1 $ ./division -l 1000000000 -f 3.141
Division took: 10624 result: 31.837
Multiplication took: 6530 result: 31.837
C++ (Optimized with -O3)
jim@Yoper ~/prog/test1 $ ./divisionO3 -l 1000000000 -f 3.141
Division took: 1025 result: 31.837
Multiplication took: 1017 result: 31.837
|All times are GMT -5. The time now is 06:39 PM.|