在Android API 8的版本之前,如果不需要double精度,而float精度就可以满足需要,那么可以使用Android API 中提供的FloatMath函数代替Java中的Math函数,优化速度。
Android最新提示:如果只需要在Android API 8以及以上版本使用APP的话,可以直接是用Java提供的Math函数,因为最新的JIT已经对Math函数做了优化,除了占用内存double比float更多以外,使用Java's Math函数更快。
下面是Android官方最新的关于使用浮点型的性能提示
Avoid Using Floating-Point
As a rule of thumb, floating-point is about 2x slower than integer on Android-powered devices.
In speed terms, there's no difference between float
and double
on the more modern hardware. Space-wise,double
is 2x larger. As with desktop machines, assuming space isn't an issue, you should prefer double
to float
.
Also, even for integers, some processors have hardware multiply but lack hardware divide. In such cases, integer division and modulus operations are performed in software—something to think about if you're designing a hash table or doing lots of math.
在Android API文档的FloatMath函数中,首句有提示
Math routines similar to those found in
Math
. On versions of Android with a JIT, these are significantly slower than the equivalent
Math
functions, which should be used in preference to these.
这里的Math函数与Java中的Math函数类似。在带有JIT(Java即时编译器)的Android版本中(也就是Android API8及以上版本),这些方法要比Java中的相应的函数要慢很多,所以我们优先选择Java里面的Math函数。
当然,这要牺牲部分内存资源。