Test: recurison vs. not (trivial)
What is the cost of a recursive algorithm vs. a non-recursive one?
View test history (1) Run this test again Return to test menuResult: Saved
The test labeled "recursive" was the faster by 0.0121 seconds, (1.719% faster)
The recursive test took 0.6928 seconds.
The non-recursive test took 0.7049 seconds.
Nitty-Gritty
Each test case ran 20 random code order iterations consisting of 130,928 loops for a total of 2,618,560 runs.
- Line execution difference (0.000005) milliseconds.
- Avg difference (0.606) milliseconds per 130,928 loops.
- Total difference 12.12 milliseconds for 2,618,560 loops
The iteration variablity for Code 1 was (7.0068) milliseconds and Code 2 was (7.0009) milliseconds. The lower and the closer together there values are the more accurate the results are.
Code
The first test, "non-recursive", was:
/* function recurse_not($i) { $result = 0; while ($i > 0) { $result += $i; $i -= 5; } return $result; } */ $result = recurse_not(5000);
The second test, "recursive", was:
/* function recurse_normal($i) { if ($i > 0) return $i + recurse_normal($i - 5); else return 0; } */ $result = recurse_normal(5000);