开发者

What the fastest way in java to receive absolute value of double?

I use code like:

Double getabsValue(final Object[] value){

if (value==null) return null;
if (value.lengt==0) return null;

f开发者_运维知识库inal 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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜