Q. Compare and contrast the use of iteration and recursion in terms of memory space and speed.
Answer =
In iteration, the code is executed repeatedly using the same memory space. That is, the memory space allocated once, is used for each pass of the loop.
On the other hand in recursion, since it involves function call at each step, fresh memory is allocated for each recursive call. For this reason i.e., because of function call overheads, the recursive function runs slower than its iterative counterpart.
On the other hand in recursion, since it involves function call at each step, fresh memory is allocated for each recursive call. For this reason i.e., because of function call overheads, the recursive function runs slower than its iterative counterpart.
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )