It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.
If the size of the collection does not change during the iteration, it is
generally a good practice to compute it beforehand, and not on each iteration:
for($i=0;$i<count($array);$i++){// calls count() on each iteration}// Betterfor($i=0,$c=count($array);$i<$c;$i++){// calls count() just once}
Loading history...
12
$dist += pow($a[$n] - $b[$n], 2);
13
}
14
15
return sqrt($dist);
16
}
17
18
/**
19
* @param array<array<float>> $points
20
* @return array<float>
21
*/
22
function find_centroid(array $points): array
23
{
24
$centroid = [];
25
26
foreach ($points as $point) {
27
foreach ($point as $n => $value) {
28
$centroid[$n] = ($centroid[$n] ?? 0) + $value;
29
}
30
}
31
32
foreach ($centroid as &$value) {
33
$value /= count($points);
34
}
35
36
return $centroid;
37
}
38
39
/**
40
* The standard Box–Muller transform generates values from the standard normal
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: