So, here is a strange question. I am working on a kNN issue and have to find the closest neighbor. I look into the distance, but again, I do not care about the actual distance, which is closest. However, since the distance cannot be negative, I need to either square or accept the absolute value of the distance.
So, here are two options for how to do this:
//note: it 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 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 information as quickly as possible. And although I understand that in the guts the power of the two can be realized with a shift, I'm not sure that this is so in a high-level language such as Java, where it is translated for the JVM. Is there a reason why one is better than the other?
A=[0,0,0], B=[1,1,1], C=[0,0,2]. A? B C? , . . , , , , , .
A=[0,0,0]
B=[1,1,1]
C=[0,0,2]
A
B
C
-, - . , for(int i = 0; i < (numAttributes - 1); i++), .
for(int i = 0; i < (numAttributes - 1); i++)
-, Math.pow(a,2) a*a, , .
Math.pow(a,2)
a*a
-, i < (numAttributes - 1)? i < numAttributes??
i < (numAttributes - 1)
i < numAttributes