Skip to content

Commit 595fe77

Browse files
committed
count() result should be precalculated before the loop to avoid the overhead of executing the function n times.
1 parent 2c613fa commit 595fe77

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

FaceDetector.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,18 @@ protected function detectOnSubImage($x, $y, $scale, $ii, $ii2, $w, $iiw, $inv_ar
240240

241241
$vnorm = $vnorm > 1 ? sqrt($vnorm) : 1;
242242

243-
for ($i_stage = 0; $i_stage < count($this->detection_data); $i_stage++) {
243+
$count_data = count($this->detection_data);
244+
245+
for ($i_stage = 0; $i_stage < $count_data; $i_stage++) {
244246
$stage = $this->detection_data[$i_stage];
245247
$trees = $stage[0];
246248

247249
$stage_thresh = $stage[1];
248250
$stage_sum = 0;
249251

250-
for ($i_tree = 0; $i_tree < count($trees); $i_tree++) {
252+
$count_trees = count($trees);
253+
254+
for ($i_tree = 0; $i_tree < $count_trees; $i_tree++) {
251255
$tree = $trees[$i_tree];
252256
$current_node = $tree[0];
253257
$tree_sum = 0;
@@ -261,7 +265,9 @@ protected function detectOnSubImage($x, $y, $scale, $ii, $ii2, $w, $iiw, $inv_ar
261265
$rects = $current_node[1];
262266

263267
$rect_sum = 0;
264-
for ($i_rect = 0; $i_rect < count($rects); $i_rect++) {
268+
$count_rects = count($rects);
269+
270+
for ($i_rect = 0; $i_rect < $count_rects; $i_rect++) {
265271
$s = $scale;
266272
$rect = $rects[$i_rect];
267273
$rx = ($rect[0]*$s+$x)>>0;

0 commit comments

Comments
 (0)