Discuss Scratch

mcjohnso
Scratcher
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”)

Last edited by mcjohnso (Oct. 6, 2013 16:55:21)

Gravitation
Scratcher
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
Scratcher
47 posts

Inefficiency in for loops

Gravitation wrote:

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.
Well that explains it. Shouldn't there be some way of not making it refresh the screen each time, like functions?
Hardmath123
Scratcher
1000+ posts

Inefficiency in for loops

Make a custom block and make it “run without screen refresh”.
mcjohnso
Scratcher
47 posts

Inefficiency in for loops

Hardmath123 wrote:

Make a custom block and make it “run without screen refresh”.
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”
joefarebrother
Scratcher
500+ posts

Inefficiency in for loops

mcjohnso wrote:

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
Scratcher
1000+ posts

Inefficiency in for loops

joefarebrother wrote:

mcjohnso wrote:

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
Scratcher
500+ posts

Inefficiency in for loops

drmcw wrote:

joefarebrother wrote:

mcjohnso wrote:

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?
Yes, he is, but custom blocks still support tail recursion…
drmcw
Scratcher
1000+ posts

Inefficiency in for loops

joefarebrother wrote:

drmcw wrote:

joefarebrother wrote:

mcjohnso wrote:

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?
Yes, he is, but custom blocks still support tail recursion…
Do they? How?
DigiTechs
Scratcher
500+ posts

Inefficiency in for loops

drmcw wrote:

joefarebrother wrote:

drmcw wrote:

joefarebrother wrote:

mcjohnso wrote:

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?
Yes, he is, but custom blocks still support tail recursion…
Do they? How?

Run the block in the block definition.
drmcw
Scratcher
1000+ posts

Inefficiency in for loops

DigiTechs wrote:

drmcw wrote:

joefarebrother wrote:

drmcw wrote:

joefarebrother wrote:

mcjohnso wrote:

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?
Yes, he is, but custom blocks still support tail recursion…
Do they? How?

Run the block in the block definition.

Ok should probably have put how do you know it's tail recursive and not stack?
DigiTechs
Scratcher
500+ posts

Inefficiency in for loops

drmcw wrote:

DigiTechs wrote:

drmcw wrote:

joefarebrother wrote:

drmcw wrote:

joefarebrother wrote:

mcjohnso wrote:

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?
Yes, he is, but custom blocks still support tail recursion…
Do they? How?

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
Scratcher
1000+ posts

Inefficiency in for loops

DigiTechs wrote:

drmcw wrote:

DigiTechs wrote:

drmcw wrote:

joefarebrother wrote:

drmcw wrote:

joefarebrother wrote:

mcjohnso wrote:

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?
Yes, he is, but custom blocks still support tail recursion…
Do they? How?

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.
Scratch also does stack style recursion, but it's pretty slow.
Jens
Scratcher
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
Scratcher
500+ posts

Inefficiency in for loops

drmcw wrote:

DigiTechs wrote:

drmcw wrote:

joefarebrother wrote:

drmcw wrote:

joefarebrother wrote:

mcjohnso wrote:

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?
Yes, he is, but custom blocks still support tail recursion…
Do they? How?

Run the block in the block definition.

Ok should probably have put how do you know it's tail recursive and not stack?
It's tail recursive if there is nothing underneath it and its not in a loop.
drmcw
Scratcher
1000+ posts

Inefficiency in for loops

joefarebrother wrote:

drmcw wrote:

DigiTechs wrote:

drmcw wrote:

joefarebrother wrote:

drmcw wrote:

joefarebrother wrote:

mcjohnso wrote:

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?
Yes, he is, but custom blocks still support tail recursion…
Do they? How?

Run the block in the block definition.

Ok should probably have put how do you know it's tail recursive and not stack?
It's tail recursive if there is nothing underneath it and its not in a loop.
Ok but I was more interested in what Scratch does under the covers can it optimise that or not?
scimonster
Scratcher
1000+ posts

Inefficiency in for loops

drmcw wrote:

joefarebrother wrote:

drmcw wrote:

DigiTechs wrote:

drmcw wrote:

joefarebrother wrote:

drmcw wrote:

joefarebrother wrote:

mcjohnso wrote:

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?
Yes, he is, but custom blocks still support tail recursion…
Do they? How?

Run the block in the block definition.

Ok should probably have put how do you know it's tail recursive and not stack?
It's tail recursive if there is nothing underneath it and its not in a loop.
Ok but I was more interested in what Scratch does under the covers can it optimise that or not?

Jens wrote:

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: http://scratch.mit.edu.ezproxyberklee.flo.org/projects/10040430/
drmcw
Scratcher
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.

Powered by DjangoBB