Loops vs recursion | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 38

Loops vs recursion

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

29th Jan 2019, 12:07 PM
ABADA S
ABADA S - avatar
14 Answers
+ 15
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/
22nd Mar 2019, 3:51 PM
Lord Krishna
Lord Krishna - avatar
+ 5
30th Jan 2019, 3:44 PM
AU Hameed
AU Hameed - avatar
+ 5
I think both are good, depending on factors you assess such as ease of programming and time saving.
31st Jan 2019, 3:59 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 5
Basil Looking at the code it looks quite long and convoluted. But, the steps written for the algorithm do not seem hard.
22nd Mar 2019, 4:15 PM
Lord Krishna
Lord Krishna - avatar
+ 3
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).
6th Jun 2019, 5:37 AM
Elwe Thor
Elwe Thor - avatar
+ 3
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.
20th Nov 2019, 11:56 AM
Sergey Soulov
Sergey Soulov - avatar
+ 2
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.
30th Jan 2019, 12:45 AM
Rafael S Valle
Rafael S Valle - avatar
+ 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.
23rd Mar 2019, 1:55 AM
Roger Greenlaw
+ 1
it depends on the use and both are best
14th Mar 2023, 4:23 PM
Ashuftah Shigri
Ashuftah Shigri - avatar
+ 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.
19th Jun 2023, 10:16 AM
Mordecai ABP
Mordecai ABP - avatar
0
Loops totally.
8th Nov 2019, 12:17 AM
Sai
Sai - avatar
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
27th Apr 2020, 9:19 AM
Sachin Waghmare
Sachin Waghmare - avatar
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. 😁
16th May 2021, 10:10 AM
Pranjal
Pranjal - avatar
- 1
vnf obokvn
19th Jun 2022, 1:51 PM
Eko Redmi