java中接收double的绝对值最快的方法是什么?

发布于 2024-12-09 20:26:25 字数 411 浏览 1 评论 0原文

我使用如下代码:

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 技术交流群。

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

发布评论

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

评论(2

过度放纵 2024-12-16 20:26:25

好吧,您目前获得的代码甚至无法编译 - 据我所知,没有 Math.abs(Object) 调用。然而,假设您实际上在那里有一个Double的演员,那么您将一直在拳击。当值已经大于 0 时,您可以避免装箱,并且当值 0 时避免调用,如下所示:

static Double getAbsValue(Object[] values) {
    if (values == null || values.length == 0) {
        return null;
    }
    Double value = (Double) values[0];
    return value > 0 ? value
         : value == 0 ? null
         : -value;
}

当我们到达最终选项时,我们已经知道该值是负数,所以我们真的不再需要调用 abs 了。

目前还不清楚这里的上下文是什么。您说您遇到了性能问题,但是这段代码中肯定是这样吗?

编辑:您的最新代码显示:

if (absValue>0) return absValue
else return -1*absValue;

这不会做同样的事情 - 如果数组包含装箱的 0 值,它不会返回 null,就像您的原始代码一样。

您应该首先关注正确性,然后再关注性能。

您希望代码对输入 0 执行什么操作?如果你希望它返回 0,那么我会使用:

return value >= 0 ? value : -value;

如果你希望它返回 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 to Double 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:

static Double getAbsValue(Object[] values) {
    if (values == null || values.length == 0) {
        return null;
    }
    Double value = (Double) values[0];
    return value > 0 ? value
         : value == 0 ? null
         : -value;
}

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:

if (absValue>0) return absValue
else return -1*absValue;

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:

return value >= 0 ? value : -value;

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.

陌路终见情 2024-12-16 20:26:25

我使用的代码如下:

Double getabsValue(final Object[] value){

为什么?

我要做的第一件事就是重新定义签名。

  • 当它基本上必须是一个 Double[] 或至少一个 Object[] 时,指定 Object[] 是没有意义的。 >包含双精度数,否则会抛出ClassCastException。
  • 当只使用第一个元素时为什么要指定数组?
  • 当您真正需要的是 double 时,为什么要使用 Double 呢?

因此,我将其重新定义为采用 double 类型的单个参数。这会将从数组中获取内容的开销转移回调用者可以看到的地方。他甚至可能没有数组,因此他必须构造它才能调用此方法。他可能已经有一个 double 而不是 Double,在这种情况下,他或编译器必须再次将其装箱为 Double。

我要做的第二件事是回顾这次更改后剩下的内容。该更改消除了 null 和长度检查以及类型转换,因此您剩下的就是 return Math.abs(d); 因此很明显,整个方法基本上是毫无意义的。

所以我要做的第三件事就是删除它。

I use code like:

Double getabsValue(final Object[] value){

Why?

The first thing I would do with this is redefine the signature.

  • It is pointless to specify Object[] when it basically has to be a Double[], or at least an Object[] which contains Doubles, otherwise it will throw a ClassCastException.
  • Why specify an array when you only use the first element?
  • Why a Double when what you really need is a double?

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.

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