Skip to content

Commit 5cf50b6

Browse files
authored
Merge pull request #1245 from MB-Finski/patch-3
Optimize distance calculation algorithm
2 parents 3187b59 + 466ddae commit 5cf50b6

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

lib/Clustering/SquaredDistance.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ public function compatibility(): array {
4545
public function compute(array $a, array $b): float {
4646
$distance = 0.0;
4747

48-
foreach ($a as $i => $value) {
49-
$distance += ($value - $b[$i]) ** 2;
48+
$count = count($a);
49+
for ($i = 0; $i < $count; $i++) {
50+
$diff = $a[$i] - $b[$i];
51+
$distance += $diff * $diff;
5052
}
5153

5254
return $distance;

0 commit comments

Comments
 (0)