Recursion vs. Iteration: Elegance or Speed?
Repeated operations are handled either by recursion or loops (iteration). One of the fundamental structures of programming is loops, and we programmers tend to think, If I need to repeat an operation, I use a loop. Loops are built-in structures of most languages, and ActionScript 3.0 has multiple options when it comes to loops. ActionScript 3.0 has the standard for (and variations), while, and do statements as well as looping methods for arrays and vectors. So if we need to repeat a set of operations, we have several iterative statements that will get the job done for us.
Why Bother With Recursion?
In most (but certainly not all) situations, iteration is faster than recursion, and so why should we even consider recursion? Recursion is nothing more than a method that can call itself that uses more memory than iteration and fills up the stack quickly. You’ll get a stack overflow with recursion both with big numbers and with infinite loops. Loops can handle big numbers better, and while they eventually blow a gasket with an infinite loop, they take longer to do so because iterations are not stored on the stack.
Despite what some programmers would see as the end of the conversation comparing iteration with recursion, I was able to find two good reasons to consider recursion over iteration.
- The code can be clearer and simpler
- Recursion offers more elegant solutions
With the caveat that the following is not a call to abandon loops, let’s take a look at the reasons you should consider recursion the next time you come across a task that requires repeating operations.
Clear and Simple
Sometimes we come to a problem where we want to lay out all of the options to better see what’s going on. With loops, especially with complex ones that involve nesting, we can quickly find ourselves spinning our wheels. On the other hand with recursion, we can lay out the steps in a series of statements that clearly show the options we have to consider.
For example in the previous post on this blog examining the construction and use of skip lists, the search algorithm uses recursion. In the stripped down version of the search method, we can clearly see the repeated steps taken in the search routine:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | public function searchList(searchKey:uint,layNum:int):int { this.searchKey=searchKey; if (initial) { layNum=4; searchNode=headNode; initial=false; } if (searchNode.contents==searchKey) { trace("success"); return searchNode.contents; } \ if (layNum<0) { trace("Number not found"); return -1; } if (searchNode["skip"+layNum].contents< =searchKey) { searchNode=searchNode["skip"+layNum]; } else { layNum--; } return searchList(this.searchKey,layNum); } |
The steps are clear to see how the search works its way through the skip list to find whether a given value were included:
- If it’s the first time through, begin at the top level in the head node.
- If the search key matches the node contents end the search and return the found contents.
- If the layer is below 0; end the search and inform the user that the value was not found.
- If the next node is greater than or equal to the search key, the search node becomes the next node
- If the next node is less than the search key, move down a layer
- Run function again if nothing is returned
The Recursive Vice
Recursive Linked List
A while back a comment by Timo Hannelin suggested that I use a skip list in one of the design patterns. (I looked but couldn’t find the comment, but I remember it was a good idea.) Anyway, I read the original article on 

Bill Sanders
Recent Comments