This content has been marked as final. Show 4 replies
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!
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,
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.
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).