Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » Inefficiency in for loops
- mcjohnso
-
47 posts
Inefficiency in for loops
While testing something, I noticed that recursion is just as fast as for loops. Recursion is usually the slowest way of repeating something in most programming languages, but I guess that isn't the case here. I asked someone a long time ago, but they just said “It is because the for loop checks to see if it is time to stop repeating every time it loops.” That isn't the answer, because “if then else” can check much faster than for loops for that kind of thing. It should be taking it that much time to check each time it loops. I haven't taken the time to look in the source code, but I thought I might ask here.
(by “for loop” I mean the “repeat () times block”)
(by “for loop” I mean the “repeat () times block”)
Last edited by mcjohnso (Oct. 6, 2013 16:55:21)
- Gravitation
-
100+ posts
Inefficiency in for loops
Scratch has a built-in delay in for-loops, and at the end of a for loop, as well as checking the condition, it re-renders the screen.
- mcjohnso
-
47 posts
Inefficiency in for loops
Well that explains it. Shouldn't there be some way of not making it refresh the screen each time, like functions? Scratch has a built-in delay in for-loops, and at the end of a for loop, as well as checking the condition, it re-renders the screen.
- Hardmath123
-
1000+ posts
Inefficiency in for loops
Make a custom block and make it “run without screen refresh”.
- mcjohnso
-
47 posts
Inefficiency in for loops
Actually, that's what I did. Thanks anyway, I will probably be using recursion for a lot of stuff now considering how fast it is with “run without screen refresh” Make a custom block and make it “run without screen refresh”.
- joefarebrother
-
500+ posts
Inefficiency in for loops
Recursion is usually the slowest way of repeating something in most programming languages,
That's a common misconception. In most programming languages there is tail call optimisation, so tail recursion is just as fast as a loop.
- drmcw
-
1000+ posts
Inefficiency in for loops
Recursion is usually the slowest way of repeating something in most programming languages,
That's a common misconception. In most programming languages there is tail call optimisation, so tail recursion is just as fast as a loop.
Isn't he talking about custom blocks not broadcasts?
- joefarebrother
-
500+ posts
Inefficiency in for loops
Yes, he is, but custom blocks still support tail recursion…Recursion is usually the slowest way of repeating something in most programming languages,
That's a common misconception. In most programming languages there is tail call optimisation, so tail recursion is just as fast as a loop.
Isn't he talking about custom blocks not broadcasts?
- drmcw
-
1000+ posts
Inefficiency in for loops
Do they? How?Yes, he is, but custom blocks still support tail recursion…Recursion is usually the slowest way of repeating something in most programming languages,
That's a common misconception. In most programming languages there is tail call optimisation, so tail recursion is just as fast as a loop.
Isn't he talking about custom blocks not broadcasts?
- DigiTechs
-
500+ posts
Inefficiency in for loops
Do they? How?Yes, he is, but custom blocks still support tail recursion…Recursion is usually the slowest way of repeating something in most programming languages,
That's a common misconception. In most programming languages there is tail call optimisation, so tail recursion is just as fast as a loop.
Isn't he talking about custom blocks not broadcasts?
Run the block in the block definition.
- drmcw
-
1000+ posts
Inefficiency in for loops
Do they? How?Yes, he is, but custom blocks still support tail recursion…Recursion is usually the slowest way of repeating something in most programming languages,
That's a common misconception. In most programming languages there is tail call optimisation, so tail recursion is just as fast as a loop.
Isn't he talking about custom blocks not broadcasts?
Run the block in the block definition.
Ok should probably have put how do you know it's tail recursive and not stack?
- DigiTechs
-
500+ posts
Inefficiency in for loops
Do they? How?Yes, he is, but custom blocks still support tail recursion…Recursion is usually the slowest way of repeating something in most programming languages,
That's a common misconception. In most programming languages there is tail call optimisation, so tail recursion is just as fast as a loop.
Isn't he talking about custom blocks not broadcasts?
Run the block in the block definition.
Ok should probably have put how do you know it's tail recursive and not stack?
Because Scratch is a tail call language. If it were a stack based language, then users would have to take into consideration the fact that you can get stack overflows.
- scimonster
-
1000+ posts
Inefficiency in for loops
Scratch also does stack style recursion, but it's pretty slow.Do they? How?Yes, he is, but custom blocks still support tail recursion…Recursion is usually the slowest way of repeating something in most programming languages,
That's a common misconception. In most programming languages there is tail call optimisation, so tail recursion is just as fast as a loop.
Isn't he talking about custom blocks not broadcasts?
Run the block in the block definition.
Ok should probably have put how do you know it's tail recursive and not stack?
Because Scratch is a tail call language. If it were a stack based language, then users would have to take into consideration the fact that you can get stack overflows.
- Jens
-
100+ posts
Inefficiency in for loops
AFAIK (As Far As I Know - moderator) Scratch doesn't have tail call elimination, and infinite recursion will make it run out of memory *sometime*. Check out John Maloney's explanation here: http://scratch.mit.edu.ezproxyberklee.flo.org/projects/10040430/
Last edited by Paddle2See (Nov. 11, 2013 18:31:11)
- joefarebrother
-
500+ posts
Inefficiency in for loops
It's tail recursive if there is nothing underneath it and its not in a loop.Do they? How?Yes, he is, but custom blocks still support tail recursion…Recursion is usually the slowest way of repeating something in most programming languages,
That's a common misconception. In most programming languages there is tail call optimisation, so tail recursion is just as fast as a loop.
Isn't he talking about custom blocks not broadcasts?
Run the block in the block definition.
Ok should probably have put how do you know it's tail recursive and not stack?
- drmcw
-
1000+ posts
Inefficiency in for loops
Ok but I was more interested in what Scratch does under the covers can it optimise that or not?It's tail recursive if there is nothing underneath it and its not in a loop.Do they? How?Yes, he is, but custom blocks still support tail recursion…Recursion is usually the slowest way of repeating something in most programming languages,
That's a common misconception. In most programming languages there is tail call optimisation, so tail recursion is just as fast as a loop.
Isn't he talking about custom blocks not broadcasts?
Run the block in the block definition.
Ok should probably have put how do you know it's tail recursive and not stack?
- scimonster
-
1000+ posts
Inefficiency in for loops
Ok but I was more interested in what Scratch does under the covers can it optimise that or not?It's tail recursive if there is nothing underneath it and its not in a loop.Do they? How?Yes, he is, but custom blocks still support tail recursion…Recursion is usually the slowest way of repeating something in most programming languages,
That's a common misconception. In most programming languages there is tail call optimisation, so tail recursion is just as fast as a loop.
Isn't he talking about custom blocks not broadcasts?
Run the block in the block definition.
Ok should probably have put how do you know it's tail recursive and not stack?
http://scratch.mit.edu.ezproxyberklee.flo.org/projects/10040430/AFAIK Scratch doesn't have tail call elimination, and infinite recursion will make it run out of memory *sometime*. Check out John Maloney's explanation here:
- drmcw
-
1000+ posts
Inefficiency in for loops
To prevent the quote block from getting stupidly large I'll not use it.
Yes I saw Jens post thanks. The tail call optimisation is a red herring then as far as Scratch is concerned, there just seemed to be the implication that Scratch did that.
Yes I saw Jens post thanks. The tail call optimisation is a red herring then as far as Scratch is concerned, there just seemed to be the implication that Scratch did that.
- Discussion Forums
- » Advanced Topics
-
» Inefficiency in for loops