Vlad,
Well I had a misplaced ) so my code was really only testing the boundary condition of doing 0! so it's a jaded benchmark for sure. *L*
Fixing that error I get similar results that the recursive is only 1/2 as fast as nonrecursive. Personally I wouldn't throw out the elegance of the recursive definition unless it was around an order of magnitude slower. The recursive is still doing 8300 factorials per second, which is likely to be fast enough.
If absolute performance was the main issue using code like:
x = seconds()
dimension gaFactorial[20]
gaFactorial[1] = 1
for i = 2 to 20
gaFactorial[i] = i * gaFactorial[i-1]
endfor
for i = 1 to 10000
y = gaFactorial[ ceil( rand() * 20 ) ]
endfor
? seconds() - x
Gives an order of magnitude performance improvement over the nonrecursive method.
Several threads here have discussed that easier to maintain code is by far better than complex code that runs faster. I agree with that theory. I'd rank the code complexity of these Factorial functions, from least to most complex: recursive, nonrecursive, lookup.
>You challenge me DF, and this is REALLY bad! :)