Wait 0 doesn't

Jim Hurley jhurley at infostations.com
Mon Apr 28 09:05:01 EDT 2003


This will be of little concern to most, but for me it made a big difference.

I have an application which allows students to program graphical 
solutions to science problems--using  Turtle Graphics.

For some simple problems I need to slow the drawing or it happens so 
quickly that it is impossible for the student to see what is 
happening. And so I introduce into the TG drawing script a "wait 
WaitTime" where WaitTime is a parameter chosen by the student to suit 
the problem.

Now for plotting the solution to Newton's equations of motion for an 
orbiting planet, the waitTime is set equal to zero since the number 
of steps needed to simulate the orbit accurately is relatively large. 
But I found using a wait time of 0  also to be somewhat herky-jerky, 
the planet stutters in its path around the moon.

However, I found if I comment-out the wait line altogether, the 
planetary motion is much faster (140 ticks without the "wait 0" 
rather than 200 ticks with "wait 0") and, more importantly, much 
smoother.

To demonstrate this in a simple example, consider the following 
handlers which do nothing but run through a loop 800 times:

on mouseUP
   repeat 800
     wait 0 millisecond
   end repeat
end mouseUP

The  above takes from 500 to 1000 milliseconds to run. It varies 
widely. (If there are no instructions within the loop, it takes 
between 0 and 1 milliseconds to run the 800 repeats. Maybe the 
compiler is smart enough to realize straight off that it is wasting 
its time.)

However:

on mouseUP
   repeat 800
     myWait 0 millisecond
   end repeat
end mouseUP

on myWait temp
   if temp = 0 then exit myWait
   wait temp milliseconds
end myWait

This version takes between 10 and 11 milliseconds (on my machine). It 
is much faster and much more consistent; all loops take about the 
same time.

This anomaly with "wait 0" is most likely an artifact of the engine, 
since it applies to MC as well as RunRev.

Moral: It takes longer to "wait 0 milliseconds" than it does to 
temporize with:  "if temp = 0 then exit myWait"

Of course it is easy to see why the compiler is not prepared for the 
fool who would program a wait of 0 milliseconds.

Jim



More information about the use-livecode mailing list