LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-16-2006, 07:46 AM   #1
traene
Member
 
Registered: Jan 2005
Distribution: Archlinux, Debian, grml
Posts: 210

Rep: Reputation: 33
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.
 
Old 04-16-2006, 12:33 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 04-16-2006, 02:40 PM   #3
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
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?
 
Old 04-16-2006, 03:55 PM   #4
traene
Member
 
Registered: Jan 2005
Distribution: Archlinux, Debian, grml
Posts: 210

Original Poster
Rep: Reputation: 33
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.
 
Old 04-16-2006, 04:03 PM   #5
traene
Member
 
Registered: Jan 2005
Distribution: Archlinux, Debian, grml
Posts: 210

Original Poster
Rep: Reputation: 33
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Floating Point in kernel 2.4.28 lucs Slackware 1 10-19-2005 08:33 AM
floating point multiplication irfanhab Programming 0 12-23-2004 10:13 PM
managing floating point vince_2x Linux - General 2 09-21-2004 09:05 PM
Athlon 64 floating-point performance gain? Entropius Linux - Hardware 1 07-19-2004 05:37 AM
floating point numbers in java spyghost Programming 8 09-06-2003 06:21 PM


All times are GMT -5. The time now is 07:53 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration