LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   JAVA/C++: Performance of floating point calculations (http://www.linuxquestions.org/questions/programming-9/java-c-performance-of-floating-point-calculations-435636/)

traene 04-16-2006 07:46 AM

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.
Happy Easter.

ta0kira 04-16-2006 12:33 PM

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.
ta0kira

addy86 04-16-2006 02:40 PM

Quote:

Originally Posted by traene
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.

Uuh, how do you calculate the reciprocal of a value? 1 / value? Don't you need a division for this?

traene 04-16-2006 03:55 PM

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.

traene 04-16-2006 04:03 PM

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:)
value: 100.0
factor: 3.14
loops: 1000000000
Division took: 9126 result: 31.84713375796178
Multiplication took: 6263 result: 31.84713375796178


value: 100.0
factor: 3.14
loops: 1000000000
Division took: 9630 result: 31.84713375796178
Multiplication took: 6627 result: 31.84713375796178


value: 100.0
factor: 3.14
loops: 1000000000
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
value: 100
factor: 3.141
loops: 1000000000
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
value: 100
factor: 3.141
loops: 1000000000
Division took: 1025 result: 31.837
Multiplication took: 1017 result: 31.837


All times are GMT -5. The time now is 09:57 AM.