Loops vs recursion | Sololearn: Learn to code for FREE!


Loops vs recursion

Which is better? Are loops faster than recursion? Is recursion bad way to programming? Is recursion cost too much memory?

1/29/2019 12:07:26 PM


14 Answers

New Answer


1. Which one is better? -> For programs which are small and aren't compute intensive use the one you prefer. When speed & memory becomes an issue consider looping. 2. Are loops faster than recursion? -> Loops are generally faster than recursion(excluding when compilers can optimize into a loop) in most languages. 3. Is recursion bad? -> Recursion is not necessarily bad; It's the performance(too long to compute) and memory usage which is. About Tail-recusion: 1. While compilers (interpreters don't do this) can eliminate the stack frames. You can't get a stack trace -- debug without stackframes. 2. If the compiler doesn't automatically do tail-call-elimination. You need to manually do it by adding an accumulator, decreasing the count. Which makes it a little complicated and harder to grasp compared to regular recursion. Hey Basil, Iterative version of "Tower of Hanoi" ;) https://www.geeksforgeeks.org/iterative-tower-of-hanoi/


I think both are good, depending on factors you assess such as ease of programming and time saving.


You can find ur answer here 👉https://www.quora.com/Is-recursion-faster-than-loops


Basil Looking at the code it looks quite long and convoluted. But, the steps written for the algorithm do not seem hard.


I’m not a programmer (‘cause I’m more a system programmer) but, more or less, I’m programming since 40 years and I really used recursion, into a production code, one time only (finding it perfect for that case).


There are some things the loops just can't do. Lets say you have an array which has unknown levels of sub arrays. To create a loop to ittirate these levels will become huge and complicated. In this case the recursion will be able to do this while remaining very simple. For the same task, recursion will need a third of the code to do the same thing as loop. Also, the philosophy of recurtion is different. Recurtion is more object oriented. Meaning that in recurtion you take an object, work on it, and if you have an other object to work on, you create a new recurtion to work on that object until you have no more other objects. The memory it uses depends on language you use. Languages like LISP for example have no problems with memory since they have a special way to execute the recursion.


I think as more complex is the stop condition, more recommended will be the use of recursion. For simple conditions, i believe there is no need to use recursion, unless you reduce the Code. A good exemple of the use of recursion, in my view, is in therms that involve "Fork and Join" to optimize tasks by taking advantage of multiprocessing.


If you are more comfortable creating loops or functions you will find most coding easiest in your comfort zone. Recursion uses more stack memory than loops but uses less program memory. Everything in programming is a tradeoff: simplicity, speed, reusability, memory and ease of debugging are just a few things to be considered. Sometimes the simplest solution is also the fastest and most efficient method, while other times the more complex and elegant solutions are faster. Select what's comfortable for you, get it working, then try to improve upon it. Well documented code during testing will also be easier to fix or modify for other purposes later.


Loops totally.


Definately loop ..becoz in recurtion after every call of recursive function stack frames are created..if the stack size less there is chances to occur the segmentation fault


Loops are generally better in memory consumption and runtime then recursion Also recursion is way to hard to implement as compare to looping Looping can solve more ques compair to recursion Recursion is like brain exercising way to do programs. 😁


vnf obokvn