## Test: recurison vs. not (trivial)

What is the cost of a recursive algorithm vs. a non-recursive one?

Run this test again Return to test menu## Historical Results

Based on (5) saved test runs, The test labeled "**non-recursive**" was (on average) the faster by

*0.0151*seconds, (1.330% faster)

The **non-recursive** test took **1.1233** seconds.

The **recursive** test took **1.1384** seconds.

## Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 265,285 loops for a total of 5,305,704 runs.

- Line execution difference (0.000003) milliseconds.

- Avg difference (0.000) milliseconds per 265,285 loops.

- Total difference 15.14 milliseconds for 5,305,704 loops

The iteration variablity for Code 1 was (0.0000) milliseconds and Code 2 was (0.0000) 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);