4 Replies Latest reply on Aug 5, 2007 6:33 PM by jpwrunyan

    Recursive functions vs. loops in AS 3

    jpwrunyan Level 1
      This is just a question out of interest, but...

      It is often easier for me to write self-calling recursive functions than concentrate on figuring out how to make a loop to do the same thing in one function. But it seems according to everything I have read that looping would be far better for the program, especially if speed is a factor. But what about memory? I would assume that faster execution speed would also mean less memory needed. How do recursive functions and loops compare to this in AS 3.0?

      If anyone has any links to a good article (in AS 3.0!) that would be nice.

      By the way, for an example of a process that can be done recursively and via a loop, refer to:
      Euclidean algorithm
        • 1. Re: Recursive functions vs. loops in AS 3
          AndyWW Level 1
          If I were you I'd just use whichever approach feels more comfortable to you. It is unlikely that using recursion versus a single function will make any noticeable difference in performance. One of the last things that you should be concerned about, at least with respect to computational speed, is optimization (this does not hold so true for database access, network response, other areas where the CPU is not the gating factor). You are dependent upon the speed of stack operations with recursion; but if you put the loop into the function then you have to keep track of "where you are", and it is sometimes more natural to let the computer handle that!

          Just my $0.02 worth!

          • 2. Re: Recursive functions vs. loops in AS 3
            Kenny Yates Level 2
            I have to agree with Andy.

            It's been my experience that either works fine for specific needs.
            Whatever feels right to you, use that.

            The only situation right off that I can think of would be muliple conditional statements (basically multiple if then elses) vs. a Switch statement.
            Switch statements "should" run faster simply because you have the ability to "break" out of them when the condition is met.
            Not to mention the ability to default to something.

            Other than that.....run with what you feel good with.

            Hope this helps,
            • 3. Re: Recursive functions vs. loops in AS 3
              If your problem is computationally hard I think a loop makes sense ... I'm sure the stack space in actionscript is limited (compared to a regular computer) if you are running deep recursion you may run into problems. If all you want to do is make things run faster then I think it doesn't really matter which approach you use.
              If you use recursion more memory is used because extra space is needed in the VM stack to store instruction pointers and local variables.
              • 4. Re: Recursive functions vs. loops in AS 3
                jpwrunyan Level 1
                Thanks for the feedback, guys.
                The note on if/else versus switch was also interested because I have wondered about that before. I had always assumed the difference was stylistic, though and that once a condition was met in a long if/else then it broke out. This isn't the case, though?

                Anyway, regarding the stack space vs speed, this is what I am most interested in.
                Of course, there are a variety of cases, but for now the one I am working with would be something like the Euclidean algorithm above. But it sounds like, for lengthy repetition, a safe rule of thumb would be "more stack space usage = slower speed" or "less stack space = faster speed". Does that sound right? (again, assuming a function like the one above, and an input that would require maybe average 50 iterations but possibly up to 1000s).