what's so bad about recursion?
I've read in books, and heard from other people; that recursion is a terrible decision. I sort of agree in certain situations. But say you have something like this... What's so bad about using recusion?
Code:
int i = 0;
if (i >= 10)
{
}
else
{
System.out.println(i + "");
i++;
}
vs some looping like this:
Code:
int i = 0;
while (i != 10)
{
System.out.println(i + "");
i++;
}
I just don't understand why recursion is a worse decision than choosing a looping or iterative type structure...
<Moderator added green checkmark in the first thread>
Re: what's so bad about recursion?
1) Because it's slower. Calling a function takes time, the so-called function overhead. In Java, this overhead is particularly high.
2) Because it takes stack space. A very simple experiment: write a loop with no termination condition. The app will run forever. Write a recursion with no termination condition. The app will soon crash.
Under some circumstances, this crash can occur under normal operation, too.
Code:
for(;;) {
}
void fn() {
fn();
}
Re: what's so bad about recursion?
Ok, so let me get this right; the reason the infinite recursive function crashes, is because the system runs out of allocated space for it..Is this right?
Also, why wouldn't the infinite looping program not crash? How come it requires less stack space?
Re: what's so bad about recursion?
1) Yes. The system only allows so much stack space to be used. Once a stack overflow occurs, the app is terminated. (In Java, I think a StackOverflowError is thrown.)
2) The loop doesn't use the stack. At no point is anything pushed on the stack. If you have variables within the loop, they are pushed on the stack as a loop iteration starts and popped off as it ends.
With the function, at least the return address is stored. And it is not popped off until the function returns, which never happens.
Re: what's so bad about recursion?
Thank you CB. I think I understand now!
Re: what's so bad about recursion?
I just want to see if this is what you were talking about recursion crashing after a while. I can specifly a smaller number and it run fine, but when I get to a larger number like I have right now, I think it is crashing. Just want to see if this is what you were talking about.
Code:
import java.util.*;
class Recur1
{
long endTime;
double time;
double n1;
long startTime = System.currentTimeMillis();
public long num(long n)
{
if (n == 6000)
{
endTime = System.currentTimeMillis();
return 0;
}
else
{
System.out.println("index: " + n);
return num(n + 1);
}
}
public void getTime()
{
time = (endTime - startTime) / 1000.0;
System.out.println("Total runniing time: " + time + " seconds");
}
public static void main(String[] args)
{
Recur1 r = new Recur1();
r.num(0);
r.getTime();
}
}
Also, do I have the timings in the right spot to be timing this?
Re: what's so bad about recursion?
That's what I meant, yeah.
As for the time, no. You're not counting the time it takes to get out of all these functions that way.
You'd have to place the stop call right after the run(0) call.