Results 1 to 7 of 7

Thread: what's so bad about recursion?

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Resolved 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>
    Last edited by NoteMe; Feb 17th, 2005 at 07:12 AM.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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();
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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?

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: what's so bad about recursion?

    Thank you CB. I think I understand now!

  6. #6

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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?

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width