php 为什么当循环数增加时,执行速度比循环数小时要快?

inkz8wg9  于 2023-02-15  发布在  PHP
关注(0)|答案(1)|浏览(133)

为什么这段代码的执行时间更快:

$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";

而不是:

$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";

为什么呢?我已经循环了很多次了,但是结果还是一样
请有人给我解释一下为什么会这样?

相关问题