w2v-server.pl: improve knn with worstbest
diff --git a/w2v-server.pl b/w2v-server.pl
index cc08e4b..149dfbf 100644
--- a/w2v-server.pl
+++ b/w2v-server.pl
@@ -122,6 +122,7 @@
 
 	FILE *out=stdout;
 	*stringBuffer=0;
+	float worstbest=0;
 
 	for (a = 0; a < N; a++) bestd[a] = 0;
 	for (a = 0; a < N; a++) bestw[a][0] = 0;
@@ -175,16 +176,19 @@
 //		if (a == 1) continue;
 		dist = 0;
 		for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
-		for (a = 0; a < N; a++) {
-			if (dist > bestd[a]) {
-				for (d = N - 1; d > a; d--) {
-					bestd[d] = bestd[d - 1];
-					besti[d] = besti[d - 1];
+		if(dist > worstbest) {
+			for (a = 0; a < N; a++) {
+				if (dist > bestd[a]) {
+					for (d = N - 1; d > a; d--) {
+						bestd[d] = bestd[d - 1];
+						besti[d] = besti[d - 1];
+					}
+					bestd[a] = dist;
+					besti[a] = c;
+					break;
 				}
-				bestd[a] = dist;
-				besti[a] = c;
-				break;
 			}
+			worstbest = bestd[N-1];
 		}
 	}