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

发布于 2024-10-15 17:15:15 字数 473 浏览 6 评论 0原文

我在一些 Android Java 游戏图形中使用浮点数,但数学库 trig 函数都返回 double,所以我必须显式转换它们。

我知道浮点数的处理速度比双精度数更快,而且我不需要高精度的答案。

例如,哪个更好:

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

或者

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

我想其他问题是“如何在模拟器上测试这个,而不需要其他因素(例如PC服务)混淆该问题?”以及“无论如何,在不同的硬件上它会有所不同吗?”

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

-Frink

I am using floats for some Android Java game graphics, but the Math library trig functions all return double, so I have to explicitly cast them.

I understand that floats are quicker to process than doubles, and I do not need high precision answers.

e.g. which is better:

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

or

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

I suppose other questions would be 'how can I test this on an emulator without other factors (e.g. PC services) confusing the issue?' and 'Is it going to be different on different hardware anyway?'

Oh dear, that's three questions. Life is never simple :-(

-Frink

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乞讨 2024-10-22 17:15:15

考虑使用 FloatMath.sin() 代替。

浮点数学

数学例程类似于数学中的例程。直接对浮点值执行计算而不会产生与 double 之间的转换的开销


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

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

为性能而设计
...
就速度而言,在更现代的硬件上,float 和 double 没有区别。从空间角度来看,double 是原来的两倍。与台式机一样,假设空间不是问题,您应该更喜欢 double 而不是 float。

尽管这个人@fadden(据称是编写虚拟机的人之一)说:

为什么Android API中有这么多浮动?

没有 FPU 的设备上,单精度浮点运算比双精度浮点运算快得多 - 精度当量。因此,Android 框架提供了一个 FloatMath 类,它复制了一些 java.lang.Math 函数,但使用 float 参数而不是 double。

在最近配备 FPU 的 Android 设备上,单精度和双精度运算所需的时间大致相同,并且比软件实现快得多。 (“性能设计”页面是为 G1 编写的,需要更新以反映各种变化。

他的最后一句话(“页面...需要更新”)指的是我在上面引用了页面,所以我想知道他是否指的是我上面引用的关于“没有区别”的那句话。

Consider using FloatMath.sin() instead.

FloatMath

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


But note this blurb in the android docs:

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.

Although this guy @fadden, purportedly one of the guys who wrote the VM, says:

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.)

His last sentence ("page ... need to be updated") refers to the page I referenced above, so I wonder if he is referring to that sentence about "no difference" that I quoted above.

心碎无痕… 2024-10-22 17:15:15

这绝对取决于硬件。我对目标平台一无所知,但在当前的 PC 上,它需要相同的时间,而浮点的速度大约是 i386 上双精度的两倍。

除非您的模拟器可以报告周期计数,否则您无法找到它,因为您的 PC 的硬件与目标平台的硬件几乎没有共同之处。当目标平台是您的 PC 时,我建议 http://code.google.com/p /caliper/ 用于此微基准测试。

This is definitely dependent on the HW. I know nothing about the target platforms, but on a current PC it takes the same amount of time while floats were about twice as fast as doubles on a i386.

Unless your emulator can report the cycle count, you can't find it out as the HW of your PC has little in common with the HW of the target platform. When the target platform were your PC, than I'd recommend http://code.google.com/p/caliper/ for this microbenchmark.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文