0

Why is this code is faster the executiont time:

$start_foreach = microtime(true);
$counter = 1;
foreach (range(1, 1000) as $number) {
    $counter++;
}
$end_foreach = microtime(true);
$time_foreach = ($end_foreach - $start_foreach);
echo "\nExecution time of foreach-loop is $time_foreach second\n";

rather than:

$start_foreach = microtime(true);
$counter = 1;
foreach (range(1, 10) as $number) {
    $counter++;
}
$end_foreach = microtime(true);
$time_foreach = ($end_foreach - $start_foreach);
echo "\nExecution time of foreach-loop is $time_foreach second\n";

just why? I've been looping it many times but still give same result

Please someone explain it to me why is it so?

  • Can't reproduce. https://3v4l.org/c5Mo6 – nice_dev Feb 13 '23 at 05:33
  • What specific output do you get from running each one? – Solomon Ucko Feb 13 '23 at 05:34
  • @SolomonUcko My lecturer want me to explain di microtime for every loop. What makes me confuse is why is faster when i change the range(1, 10) into range(1, 1000)? even tho when I change it into range(1, 10000) it run more faster. The microtime show smaller number – Brobrobrebre Feb 13 '23 at 05:37