gpt4 book ai didi

java - 使用 FloatMath 或 Math 和类型转换?

转载 作者:IT老高 更新时间:2023-10-28 20:52:09 32 4
gpt4 key购买 nike

在 Android API 的最新更新中,FloatMath标有以下 lint-warning:

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.

还有提到here在最近的硬件上,double 和 float 在速度上是相等的。

我正在我目前正在处理的应用程序中使用一些三角数学(针对 Froyo 及更高版本),但不需要高精度,所以到目前为止我一直在使用 float 和 FloatMath,并且没有必要切换到 double 。
但是,“使用 Math 而不是 FloatMath” - 建议并没有说明如果 float 是所需的结果则使用哪一个。

所以,简而言之;哪个更可取?

float foo = FloatMath.sin(bar);

float foo = (float) Math.sin(bar);

顺便说一句,我只有一个 Froyo 设备,所以我自己无法真正进行任何适当的基准测试。

从 API 级别 22 开始,FloatMath 类已被弃用,取而代之的是常规 Math 类。

最佳答案

从下面的结果可以看出,使用 java.lang.Math 处理 float 比使用 double 更快,并且比 FloatMath 更快。此外,FloatMath 在 API 级别 17 之前没有 .exp() 或 .pow()。

在三星 GT_i9295 (4.2.2) 上,2^24 个周期

Math.exp(D)      Total:     7405 ms,     Per Op: 0.0004414 ms
(F)Math.exp(F) Total: 5153 ms, Per Op: 0.0003071 ms
FloatMath.exp(F) Total: 8533 ms, Per Op: 0.0005086 ms

三星上没有 Math.sin 的数据,因为它随机决定忽略 Log.d() >:(

在 HTC Hero_HT99VL (2.3.7) 上,2^12 个周期

Math.sin(D)      Total:       42 ms,     Per Op: 0.0102539 ms
(F)Math.sin(F) Total: 33 ms, Per Op: 0.0080566 ms
FloatMath.sin(F) Total: 38 ms, Per Op: 0.0092773 ms

Math.exp(D) Total: 56 ms, Per Op: 0.0136719 ms
(F)Math.exp(F) Total: 47 ms, Per Op: 0.0114746 ms

FloatMath.exp()、.pos() 和 .hypot() 需要 API 级别 17

关于java - 使用 FloatMath 或 Math 和类型转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15143735/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com