This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2008-01-15 08:53:30

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Optimizing Program Execution Speed

A lot of the programs done in Scratch are Arcade-style games that benefit from fast execution.  Does anybody have any ideas on how to optimize your programs for speed?  Some specific questions:

1) Does it negatively impact speed when you modularize your code using Broadcast Messages?  How much?

2) I noticed that the first time a looping script runs, it takes longer than the second and subsequent times.  When is the script converted to Bytecode (or is it?) and is there any benefit to doing a "dummy run" of a script to force the Bytecode conversion before it is actually used in a critical area?

3) Which is less resource intensive, using "color sensing" or "touching sprite" sensing?

4) What about those Forever Loops, are they more resource intensive than the Wait Until construct?

5) How much does it cost you to add another Sprite?  Should I be trying to make one sprite do many different things in order to keep the sprite count down?

Any help would be appreciated, thanks.


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#2 2008-01-15 09:46:02

Jens
Scratcher
Registered: 2007-06-04
Posts: 1000+

Re: Optimizing Program Execution Speed

My own experience on some of these issues has led to mixed results:

1) I've noticed that using a lot of broadcasts did not slow down my projects at all, as compared to using large stacks.

2) I did not yet notice that loop-execution takes longer the first time. I'm not sure about the java player but am pretty certain, that Scratch-projects are, in fact, never converted to byte-code at all, but interpreted all along. Nonetheless there are many reasons why a script may take slightly longer to execute the first time, especially if it uses resources like costumes or midi-ports which need to be initialized first.

3) I don't think there's any difference in system-resource usage between color sensing and touching-sprite sensing in the Squeak version.

4) I have noticed, that running several forever-loops in parallel will significantly and sometimes spectacularly slow down the execution of a project. Therefore reducing the number of active threads in my experience seems to be the single most effective way to speed up project execution. But there are some catches to this as well. For example, I generally like to trigger sprite movement by a keyboard event. But some users (I think it was archmage) have observed, that a sprite responds more quickly and smoothely if it queries the key-pressed event from within a constantly active forever-loop. So, I guess it really depends on what exactly you want to speed up...

5) The biggest surprise for me was that there seems to be a *huge* difference between the Squeak-version and the online Java-player when it comes to many sprites. In the offline Squeak-version I was baffled to find out that my projects got *faster* if I distributed my code over more sprites, whereas in the Java-player even single-sprite projects that sometimes don't work in the Squeak version at all seem to be doing okay. It is my overall impression that adding sprites 'costs' next to nothing in the Squeak version, but slows down the Java-player.

These are - however - just my impressions from using Scratch. I didn't inspect the source code (yet) to find out about performance profiling of Scratch projects, as I'm generally more interested in cool ideas than in speed-games.


Jens Mönig

Offline

 

#3 2008-01-15 12:10:01

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Optimizing Program Execution Speed

Thanks Jens,  very helpful comments.  The case where I noticed the first loop iteration being significantly slower involved using the Timer and a Wait statement, I belive.  The timer could be the external resource access you were mentioning, I suppose.


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#4 2008-01-15 12:47:39

Canthiar
Scratcher
Registered: 2007-05-16
Posts: 100+

Re: Optimizing Program Execution Speed

In programs that I've written Loops and BroadcastAndWait have caused considerable slow down because the current script seems to stop executing at the end of the Loop and the start of the BroadcastAndWait to allow other scripts to run and allow for the screen to refresh.

This really depends on what kind of program you're writing as to if any of this is noticeable.

Offline

 

Board footer