error401
1000+ Head-Fier
- Joined
- Oct 11, 2006
- Posts
- 1,244
- Likes
- 11
Quote:
You're right of course, though this limitation is really due to the way the language works than an implicit disadvantage to the recursive solution. A tail recursive function in a language like Scheme can certainly be as fast or faster than an iterative function in a language like C. In most cases performance is not important enough to sacrifice the clarity of the recursive solution to a recursive problem. This should be clear from your example function, which takes much more analysis to understand (and I'm far too lazy to actually read through this to see if it's correct, though I do remember a similar solution existing). I don't recommend this approach, especially for someone new to programming, unless the performance of the particular function (and its callers) is a critical system parameter.
In your particular example, the performance difference is not due to the lack of recursion, but the elimination of the lengthy multiply instruction that removing the recursion allows. In fact, the recursive example will generate less instructions than the loop will (and makes far fewer hits to memory, though this might fit into the registers on a CISC platform like i386). C will choke before 20! with any function unless you use big integers anyway, 12! is the largest that will fit into int32, and with a stack only 12 calls deep, especially with tail call optimization you won't be able to measure a difference. Using big integers, multiply is probably even slower than it is on-cpu, and the merits become apparent. But who really needs to compute factorials at runtime anyway? In a real application you'd probably want a lookup table unless you're a numericist or something. Writing a function like this is an exercise is using recursion.
Originally Posted by Arainach /img/forum/go_quote.gif First off, do you HAVE to use recursion for the entire program? Specifically, that Factorial Program will choke on anything larger than 20 or so. It's easy to use a table to linearize factorial lookup as such: Code:
Code:
That'll fun a LOT faster than the recursive function. Recursion, done right, can be the most efficient solution. But very, very rarely is it ever so. As to the permutation problem: I'll take a look at it now. |
You're right of course, though this limitation is really due to the way the language works than an implicit disadvantage to the recursive solution. A tail recursive function in a language like Scheme can certainly be as fast or faster than an iterative function in a language like C. In most cases performance is not important enough to sacrifice the clarity of the recursive solution to a recursive problem. This should be clear from your example function, which takes much more analysis to understand (and I'm far too lazy to actually read through this to see if it's correct, though I do remember a similar solution existing). I don't recommend this approach, especially for someone new to programming, unless the performance of the particular function (and its callers) is a critical system parameter.
In your particular example, the performance difference is not due to the lack of recursion, but the elimination of the lengthy multiply instruction that removing the recursion allows. In fact, the recursive example will generate less instructions than the loop will (and makes far fewer hits to memory, though this might fit into the registers on a CISC platform like i386). C will choke before 20! with any function unless you use big integers anyway, 12! is the largest that will fit into int32, and with a stack only 12 calls deep, especially with tail call optimization you won't be able to measure a difference. Using big integers, multiply is probably even slower than it is on-cpu, and the merits become apparent. But who really needs to compute factorials at runtime anyway? In a real application you'd probably want a lookup table unless you're a numericist or something. Writing a function like this is an exercise is using recursion.