简体   繁体   English

Java Math.abs与Math.pow

[英]Java Math.abs vs Math.pow

So here's an odd question. 所以这是一个奇怪的问题。 I'm working on a kNN problem and need to find the nearest neighbor. 我正在研究一个kNN问题,需要找到最近的邻居。 I'm looking into distance, but once again, I don't care about the actual distance, just which one is closest. 我正在调查距离,但我再一次不关心实际的距离,只是哪一个距离最近。 However, since distance can't be negative, I need to either square or take the absolute value of the distance. 但是,由于距离不能为负,我需要平方或取距离的绝对值。

So here are two options for how to accomplish this: 所以这里有两个选项来完成这个:

//note: it's been abstracted for multiple dimensions (not just x and y)
for(int i = 0; i < (numAttributes - 1); i++)
{
    distance += Math.pow((a.value(i) - b.value(i)), 2);
}

and

//note: it's been abstracted for multiple dimensions (not just x and y)
for(int i = 0; i < (numAttributes - 1); i++)
{
    distance += Math.abs(a.value(i) - b.value(i));
}

My question is which is faster. 我的问题是哪个更快。 Since this is a data mining application, I want it to be able to process the information as quickly as possible. 由于这是一个数据挖掘应用程序,我希望它能够尽快处理信息。 And while I understand, that in the guts, a power of two can be implemented with a shift, I'm not sure this is the case in such a high level language like Java where it gets translated for the JVM. 虽然我明白,在内部,两个人的力量可以通过移位来实现,我不确定在像Java这样的高级语言中它是如何被转换为JVM的情况。 Is there a reason why one is better than the other? 有人为什么比另一个好?

First, consider vectors A=[0,0,0] , B=[1,1,1] , C=[0,0,2] . 首先,考虑矢量A=[0,0,0]B=[1,1,1]C=[0,0,2] Which one is closer to A ? 哪一个更接近A Is it B or C ? B还是C Actually, caring about the distance measure is absolutely crucial in kNN. 实际上,关心距离测量在kNN中绝对至关重要。 And we are only talking about Manhattan and euclidean distances. 我们只谈论曼哈顿和欧几里德的距离。 You could, by example, use the cosine similarity as well, and you should select the distance measure carefully, taking the into account the knowledge you have about your data. 例如,您也可以使用余弦相似度,并且应该仔细选择距离度量,同时考虑您对数据的了解。

Second, instead of such a low-level optimization, consider something smarter. 其次,而不是这种低级优化,考虑更聪明的事情。 Such as breaking your for(int i = 0; i < (numAttributes - 1); i++) loop as soon as too large distance is detected. 例如,一旦检测到太大的距离,就打破你的for(int i = 0; i < (numAttributes - 1); i++)循环。

Third, using Math.pow(a,2) to compute a*a is definitely very inefficient. 第三,使用Math.pow(a,2)来计算a*a绝对是非常低效的。

Fourth, i < (numAttributes - 1) ? 第四, i < (numAttributes - 1) Didn't you mean i < numAttributes ?? 你不是说i < numAttributes ??

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM