java中接收double的绝对值最快的方法是什么?
我使用如下代码:
Double getabsValue(final Object[] value){
if (value==null) return null;
if (value.lengt==0) return null;
final Double absValue=Maths.abs(value[0]);
if (absValue>0) return absValue
else return null;
但在我的应用程序中,我遇到性能问题。 如何对其进行优化?
也许更好用?
if (absValue>0) return absValue
else return absValue<0?-absValue:null;
谢谢
I use code like:
Double getabsValue(final Object[] value){
if (value==null) return null;
if (value.lengt==0) return null;
final Double absValue=Maths.abs(value[0]);
if (absValue>0) return absValue
else return null;
But in my application I have problem with performance.
How it can be optimized?
Maybe better use?
if (absValue>0) return absValue
else return absValue<0?-absValue:null;
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,您目前获得的代码甚至无法编译 - 据我所知,没有
Math.abs(Object)
调用。然而,假设您实际上在那里有一个Double
的演员,那么您将一直在拳击。当值已经大于 0 时,您可以避免装箱,并且当值为 0 时避免调用,如下所示:当我们到达最终选项时,我们已经知道该值是负数,所以我们真的不再需要调用
abs
了。目前还不清楚这里的上下文是什么。您说您遇到了性能问题,但是这段代码中肯定是这样吗?
编辑:您的最新代码显示:
这不会做同样的事情 - 如果数组包含装箱的 0 值,它不会返回 null,就像您的原始代码一样。
您应该首先关注正确性,然后再关注性能。
您希望代码对输入 0 执行什么操作?如果你希望它返回 0,那么我会使用:
如果你希望它返回 null,请使用我最初提供的代码。
顺便问一下,为什么要包含乘以 -1 而不是仅使用一元否定运算符?我希望编译器或 JIT 无论如何都能摆脱它,但从根本上来说,您不希望执行乘法 - 您希望执行求反。让你的代码读起来尽可能贴近你描述你的目标的方式。
Well, the code you've got at the moment won't even compile - there's no
Math.abs(Object)
call as far as I'm aware. However, assuming you actually have a cast toDouble
there, you're going to be boxing all the time. You could avoid boxing when the value is already greater than 0, and avoid the call when the value is 0, like this:By the time we get to the final option, we already know that the value is negative, so we don't really need the call to
abs
any more.It's not really clear what the context is here. You say you've got a performance problem, but is it definitely in this code?
EDIT: Your latest code shows:
That doesn't do the same thing - it doesn't return null if the array contains a boxed 0 value, as your original code does.
You should focus on correctness before performance.
What do you want your code to do with an input of 0? If you want it to return 0, then I would use:
If you want it to return null, use the code I provided originally.
Why include a multiplication by -1 rather than just use the unary negation operator, by the way? I would expect either the compiler or the JIT to get rid of that anyway, but fundamentally you don't want to be performing multiplication - you want to perform negation. Make your code read as closely as possible to how you'd describe your aims.
为什么?
我要做的第一件事就是重新定义签名。
Double[]
或至少一个Object[]
时,指定Object[]
是没有意义的。 >包含双精度数,否则会抛出ClassCastException。double
时,为什么要使用Double
呢?因此,我将其重新定义为采用
double
类型的单个参数。这会将从数组中获取内容的开销转移回调用者可以看到的地方。他甚至可能没有数组,因此他必须构造它才能调用此方法。他可能已经有一个 double 而不是 Double,在这种情况下,他或编译器必须再次将其装箱为 Double。我要做的第二件事是回顾这次更改后剩下的内容。该更改消除了 null 和长度检查以及类型转换,因此您剩下的就是
return Math.abs(d);
因此很明显,整个方法基本上是毫无意义的。所以我要做的第三件事就是删除它。
Why?
The first thing I would do with this is redefine the signature.
Object[]
when it basically has to be aDouble[]
, or at least anObject[]
which contains Doubles, otherwise it will throw a ClassCastException.Double
when what you really need is adouble
?So I would redefine it it to take a single argument of type
double
. That moves the overhead of getting things out of the array back to the caller where he can see it. He may not even have an array, so he would have to construct it to call this method. And he may already have a double not a Double, in which case again he or the compiler would have to box it into a Double.The second thing I would do with it is review what's left after this change. The change gets rid of the null and length checks, and the typecast, so all you are left with is
return Math.abs(d);
So it becomes clear that the entire method is basically pointless.So the third thing I would do with it is delete it.