+ 15
Is it better to use recursive or loops?
I made a code with an example where a function can be done in both ways. In a real case, which option should I go to? https://code.sololearn.com/cHuWCSn0imCY/?ref=app
23 odpowiedzi
+ 15
Both were alright look at this code by Serena Yvonne if you want to look at the time they use
https://code.sololearn.com/cbjU93D4nCR4/?ref=app
+ 11
both are okay, but if you want to go for speed, loop is faster than recursive function
+ 10
I think loop is better than recursive. Recursive is good but loop is faster.
+ 8
Werg Serium What about the link code?
+ 6
If your preferred language is available on pythontutor.com, I recommend to go there and visualize some recursive codes. Then you will see how much more "overhead" you get by using recursion. Recursion usually only makes sense in some very limited situations like using a depth first search or flattening a list where using recursion really makes it easier to code the algortihms.
+ 6
Depends on what are you trying to achive
+ 6
Generally speaking for small input size and your current algorithm it shoudn't matter--I tried 900000 and it was fine. Replacing c/2 with c-2 it chokes on 2000.
When calculating large values you will most likely blow the stack. Even if it's unlimited it will take a very long time.
You will need to resort to iteration and/or start optimising your algorithm (like using memoization, doing less work like calculating half as much as before, etc)
Even still a loop should be faster than recursion sometimes many times faster.
Try calculating fibonnacci 200000 recursively--it's best not done. I'm unable to do that even with memoization-- program will crash. The iterative version (with memoization) does the same under 5s.
#Similar discussion
https://www.sololearn.com/discuss/1671835/?ref=app
+ 5
And rarelly an recursive function can be faster than a iterative function
+ 5
Once you get used to recursion, it makes source code more readable (more eloquent) and easier to maintain. Then again, it is a matter of preference😊
+ 4
Its depends of what you want to do.
+ 4
Thoq! This is turning into debate about resource efficiency (speed & stack overflow) vs source code (eloquence) maintainability. In the end, I feel that there is no right or wrong answer, just preference and situations
+ 4
@Da2
Yes, in principle you are right. Except that in Python (which is the language of the example codes InvBoy linked) I would strongly prefer using iterative code. Python is not really built for recursion and hits the recursion limit pretty fast.
+ 4
The point is complexity... the more complex source code gets, what is important. I do not have time for a debate.... Thank you for your candid response
+ 4
Loops are a bit easier to debug if there is an error.
+ 3
Asterisk in the most of cases the loop is faster than recursive but in this case i didnt see any differente about speed.
+ 3
*AsterisK* how loop is faster than recursive function
+ 2
Loops (While is best IMHO) 😉 Recursion can cause stack overflow ....
+ 2
Depends in many cases recursion may be reasonable.
Recursion has the main advantage that in some cases will need a much shorter and simpler implementation than the iterative method.
But generally, recursions are more memory hungry and execute more instructions which will make them slower.
+ 2
Aditya Choudhury that's pretty wrong,when i work with python loops never get confusing plus there's a limit to how much you can use recursion,so loops,compressions and generators are a better alternative in most cases.
+ 2
Loop is more faster than recursive.. Because it call the function by itself but in loop the loop is working again nd again until the condition is false