是更好地转换整个表达式,还是只是不同类型的变量
我在一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑使用 FloatMath.sin() 代替。
浮点数学
但请注意android文档中的这个简介:
http://developer.android .com/guide/practices/design/performance.html#avoidfloat
尽管这个人@fadden(据称是编写虚拟机的人之一)说:
为什么Android API中有这么多浮动?
他的最后一句话(“页面...需要更新”)指的是我在上面引用了页面,所以我想知道他是否指的是我上面引用的关于“没有区别”的那句话。
Consider using FloatMath.sin() instead.
FloatMath
But note this blurb in the android docs:
http://developer.android.com/guide/practices/design/performance.html#avoidfloat
Although this guy @fadden, purportedly one of the guys who wrote the VM, says:
Why are there so many floats in the Android API?
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.
这绝对取决于硬件。我对目标平台一无所知,但在当前的 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.