+ 39
Loops vs recursion
Which is better? Are loops faster than recursion? Is recursion bad way to programming? Is recursion cost too much memory?
13 Réponses
+ 16
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/
+ 5
You can find ur answer here 👉https://www.quora.com/Is-recursion-faster-than-loops
+ 5
I think both are good, depending on factors you assess such as ease of programming and time saving.
+ 5
Basil
Looking at the code it looks quite long and convoluted. But, the steps written for the algorithm do not seem hard.
+ 4
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.
+ 3
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.
+ 2
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.
+ 2
it depends on the use and both are best
+ 1
THE VOICE OF REASON
Basically, a recursion is just a loop in disguise.
Recursion(mathematics) is an expression such that each term is generated by repeating a particular mathematical operation
In programming, a recursion has a complex set of variations which are, at the same time, based on interconnected parts.
In my humble opinion, loops are simpler as compared to recursions. On the point of which is better, it's dependent on programmer and the programme in question.
0
Loops totally.
0
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
0
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. 😁
- 1
vnf obokvn