Discuss Scratch
- DadOfMrLog
-
Scratcher
1000+ posts
Recursion. -BY ME
Yeah, It's true recursion normally bets beaten by everything in speed, but if you test it out in a very laggy project (like this one) you'll find it'll sometimes (yeah, just sometimes) beat repeating.
Hmmmm, I think you've got confused over how Scratch work with loops…
What you're timing here has nothing at all to do with speed of operations in loops vs recursion - you're only comparing how the internal workings of Scratch schedule multiple simultaneous toops with other operations.
The times you're seeing in your project for the two cases are really not related to how long it actually needs to perform those operations. Try switching off screen refresh for your recursion block and you'll see just how fast it really can whizz through that list calculation.
And now take your calculation loop out of the main script and put it into its own custom block, with refresh switched off. You're gonna need to do about 1000x as much work now (100000 list items) so you can see a difference in the times…!
Replace the define blocks with broadcast.
Oh.
Okay…
Have you tried that? I think you'll find there's a serious problem with it because of the way that Scratch cuts off a broadcast receive script when it starts it again… Besides, if it did work in the way you intend, it's still really a form of recursion.
EDIT: actually, it will happen to work fine in this case, because it doesn't have anything else after each broadcast, so it doesn't matter that they get ‘chopped’ when the same one starts again. But if there was some form of collation of data immediately after (at least) one of the broadcasts then it would fail to get to that.
EDIT2: Actually, it occurred to me that the above-mentioned feature means I think you *can* rewrite those example scripts as a single repeat loop…?
Single recursion (i.e. where A only calls A) can always be rewritten as some form loop (though that doesn't necessarily mean it's ‘better’ that way). Multiple recursion (like your example above) can sometimes be rewritten as a loop, but not always - at least, not without emulating some form of nested ‘state’ information that acts like a typical ‘call stack’ (so it ends up being a form of user-programmed recursion).
Hope that makes sense!
Last edited by DadOfMrLog (Sept. 10, 2013 09:04:33)
- TPAL125
-
Scratcher
75 posts
Recursion. -BY ME
Yep, I've tried it.
Replace the define blocks with broadcast.
Oh.
Okay…
Have you tried that? I think you'll find there's a serious problem with it because of the way that Scratch cuts off a broadcast receive script when it starts it again… Besides, if it did work in the way you intend, it's still really a form of recursion.
And despite what I've said about def block recursion, broadcasting actually turns out to be much faster.
The times you're seeing in your project for the two cases are really not related to how long it actually needs to perform those operations. Try switching off screen refresh for your recursion block and you'll see just how fast it really can whizz through that list calculation.
V
…….I really need to stop making myself look dumb.
Sorry about that,
