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?
I'm not sure how you're running the test, but my results are different:
Run it:
https://onlinephp.io?s=fY5BCoNADEX3gncI4mLECnVti6veQijTMdaBmkicgULp3RuhQt00u5D3fv6pncc5TdIkX4KVcB1Y0LoRzjB5Jxz8hCZIxKJRxHGkgKLHWtcNNWLpjqY-QH0swC6QU5xuKAW80gR0Nq8s1XprDlL__9G6_RBmZ1Sw77oa6EaGrKPLE10MngnWCOABvlD1YJ7Ba7ld9oKOqe8oaz4%2C&v=8.2.1
https://onlinephp.io?s=fY5BCoNADEX3gncI4mLECrq2paveQijTMdaBmkicgULp3RuhQt00u5D3fv7xPI9zmqRJvgQr4TqwoHUjnGDyTjj4CU2QiEWriONIAUWPja4basTSHU1zgKau6wLsAjnF6YZSwCtNQGczy1K9tyYh9f9frdsPYXZGBfu2q4FuZMg6ujzRxeCZYI0AHuALVQ_mGbyW22Uv6Jj6jrL2Aw%2C%2C&v=8.2.1