Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Float or Double?

Which is faster, double or float, when preforming arithimic (+-*/%), and is it worth just using float for memory reasons? Precision is not an issue much of an issue.

Feel free to call me crazy for even thinking this. Just curious as I see the amount of floats I'm using is getting larger.

EDIT 1: The only reason this is under android is because that is where I believe memory matters; I wouldn't even ask this for desktop development.

like image 678
ahodder Avatar asked Jun 03 '11 20:06

ahodder


Video Answer


2 Answers

The processing speed on both types should approximately be the same in CPUs nowadays.

"use whichever precision is required for acceptable results."

Related questions have been asked a couple of times here on SO, here is one.

Edit:

In speed terms, there's no difference between float and double on the more modern hardware.

Please check out this article from developer.android.com.

like image 77
Nick Rolando Avatar answered Sep 16 '22 18:09

Nick Rolando


Double rather than Float was advised by ADT v21 lint message due to the JIT (Just In Time) optimizations in Dalvik from Froyo onwards (API 8 and later).

I was using FloatMath.sin and it suggested Math.sin instead with the following under "explain issue" context menu. It reads to me like a general message relating to double vs float and not just trig related.

"In older versions of Android, using android.util.FloatMath was recommended for performance reasons when operating on floats. However, on modern hardware doubles are just as fast as float (though they take more memory), and in recent versions of Android, FloatMath is actually slower than using java.lang.Math due to the way the JIT optimizes java.lang.Math. Therefore, you should use Math instead of FloatMath if you are only targeting Froyo and above."

Hope this helps.

like image 36
phillxnet Avatar answered Sep 16 '22 18:09

phillxnet