The do while loop calls the same PHP function as function recursion does each time it iterates the loop, because it uses it to evaluate the expression after while. Plus, it has a bit more overhead for operating the looping mechanism. So, that may explain why it's slower.
I just added this code:
stopwatch reset;
foreach a fn range 0 10000:
next;
stopwatch;
And it got the fastest result:
Elapsed time: 0.039952039718628 seconds
It saved a lot of time by never trying to evaluate a as an expression.
The do while loop calls the same PHP function as function recursion does each time it iterates the loop, because it uses it to evaluate the expression after while. Plus, it has a bit more overhead for operating the looping mechanism. So, that may explain why it's slower.
I just added this code:
And it got the fastest result:
It saved a lot of time by never trying to evaluate a as an expression.