java - 是转换整个表达式更好,还是只转换不同类型的变量更好

标签 java android types trigonometry

我正在为一些 Android Java 游戏图形使用 float ,但数学库触发函数都返回 double 值,因此我必须显式转换它们。

我知道 float 比 double 处理起来更快,而且我不需要高精度答案。

例如哪个更好:

screenXf = (float) (shipXf + offsetXf * Math.sin(headingf) - screenMinXf);

screenXf = shipXf + offsetXf * (float) (Math.sin(headingf)) - floatScreenMinXf;

我想其他问题是“我如何在没有其他因素(例如 PC 服务)混淆问题的情况下在模拟器上测试它?”以及“它在不同的硬件上会有所不同吗?”

哦,天哪,这是三个问题。生活从来都不简单:-(

-弗林克

最佳答案

考虑使用 FloatMath.sin()相反。

float

Math routines similar to those found in Math. Performs computations on float values directly without incurring the overhead of conversions to and from double.


但请注意 android 文档中的这个简介:

http://developer.android.com/guide/practices/design/performance.html#avoidfloat

Designing for Performance
...
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.

虽然这个人@fadden,据称是编写 VM 的人之一,说:

Why are there so many floats in the Android API?

On devices without an FPU, the single-precision floating point ops are much faster than the double-precision equivalents. Because of this, the Android framework provides a FloatMath class that replicates some java.lang.Math functions, but with float arguments instead of double.

On recent Android devices with an FPU, the time required for single- and double-precision operations is about the same, and is significantly faster than the software implementation. (The "Designing for Performance" page was written for the G1, and needs to be updated to reflect various changes.)

他的最后一句话(“page ... need to be updated”)指的是我上面引用的页面,所以我想知道他是不是指我上面引用的关于“没有区别”的那句话。

关于java - 是转换整个表达式更好,还是只转换不同类型的变量更好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4903239/

相关文章:

Java:解析大文件大小:NumberFormatException

java - 从应用程序中删除 xercesImpl-2.8.1.jar

java - Struts 2 Action 中的 Android GCM 服务器

java - Hadoop 应用程序的包装器

android - 撰写中的条件导航,无需单击

java - 如何在 Android Studio 中创建类文件

Android - 从 PHONEGAP 打开 native android 应用程序

dictionary - F# 类型匹配 - 无法创建映射或匹配记录

java - 通过 Java 反射的构造函数参数

使用保留名称的 typescript 命名空间声明