Wait 0 doesn't

Dar Scott dsc at swcp.com
Tue Apr 29 13:46:00 EDT 2003


On Tuesday, April 29, 2003, at 08:28 AM, Jim Hurley wrote:

> I think it is a problem of consistency. For example when I run:
>
> on mouseUP
>   put empty into field 1
>   repeat 200
>       put the milliseconds into startTime
>       wait 0 ticks --Quite variable
>      -- myWait 0  -- Quite smooth
>       put  the milliseconds - startTime & comma & space after field 1
>   end repeat
> end mouseUP
>
> on myWait temp
>   if temp = 0 then exit myWait
>   wait temp ticks
> end myWait
>
>
> I get the following list:
>
> 1, 1, 1, 7, 2, 1, 1, 2, 1, 1, 1, 3, 5, 8, 5, 9, 1, 0, 0, 1, 0, 0, 1, 
> 7, 1, 1, 1, 1, 1, 1, 0, 4, 1, 2, 1, 1, 1, 0, 0, 4, 8, 1, 1, 1, 0, 1, 
> 1, 2, 8, 0, 1, 1, 1, 1, 1, 1, 9, 1, 0, 1, 2, 1, 1, 3, 4, 8, 12, 4, 5, 
> 3, 4, 4, 4, 9, 3, 3, 2, 3, 0, 11, 3, 1, 2, 1, 0, 1, 1, 7, 1, 0, 1, 0, 
> 0, 1, 4, 1, 4, 1, 0, 0, 1, 1, 1, 6, 1, 1, 1, 0, 0, 2, 9, 1, 0, 1, 0, 
> 1, 1, 8, 1, 1, 1, 0, 1, 1, 5, 2, 2, 2, 4, 1, 1, 1, 0, 1, 2, 7, 1, 1, 
> 0, 1, 0, 7, 1, 1, 1, 1, 3, 6, 0, 2, 0, 1, 1, 3, 6, 0, 1, 1, 1, 1, 1, 
> 10, 1, 1, 1, 1, 2, 3, 3, 1, 1, 1, 1, 1, 1, 10, 3, 0, 0, 0, 3, 15, 3, 
> 2, 2, 4, 2, 8, 4, 2, 3, 3, 5, 3, 1, 2, 1, 1, 1, 8,
>
>
I get this:

2, 0, 0, 0, 1, 0, 1, 0, 2, 1, 0, 1, 0, 1, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 
0, 0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 
0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 6, 0, 0, 7, 1, 0, 0, 
1, 0, 1, 0, 2, 1, 0, 2, 0, 1, 0, 2, 0, 1, 2, 0, 0, 1, 2, 0, 0, 2, 0, 0, 
2, 0, 1, 2, 2, 2, 1, 1, 1, 5, 1, 1, 2, 2, 0, 0, 2, 1, 1, 2, 0, 0, 3, 0, 
0, 2, 1, 0, 2, 0, 0, 1, 0, 0, 5, 0, 0, 5, 1, 0, 5, 2, 1, 0, 2, 0, 1, 2, 
1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 2, 1, 1, 2, 0, 5, 2, 0, 2, 1, 0, 
3, 0, 1, 4, 1, 0, 0, 2, 0, 0, 2, 1, 0, 2, 1, 0, 2, 1, 1, 2, 0, 0, 2, 0, 
1, 3, 0, 5, 0, 0, 4, 1,

A modification:

on mouseUP
   put empty into field "report"
   set numberFormat to "0.00"
   repeat 200
       put the long seconds into startTime
       wait 0 ticks --Quite variable
       -- myWait 0  -- Quite smooth
       put the long seconds into endTime
       put  (endTime - startTime)*1000 & comma & space after field 
"report"
   end repeat
end mouseUP

With these results (note the first is usually long):

1.99, 0.25, 0.29, 0.26, 0.26, 0.26, 0.26, 0.22, 0.26, 0.26, 1.35, 0.24, 
0.26, 0.30, 0.26, 0.22, 0.26, 0.25, 0.26, 1.23, 0.28, 0.26, 0.26, 0.22, 
0.26, 0.25, 0.26, 0.22, 1.30, 0.28, 0.26, 0.22, 0.26, 0.27, 0.26, 0.21, 
0.26, 1.37, 0.27, 0.21, 0.22, 0.28, 0.25, 0.22, 0.27, 0.25, 1.35, 0.37, 
0.26, 0.26, 0.25, 0.26, 0.22, 0.26, 0.26, 1.25, 0.28, 0.26, 0.22, 0.26, 
0.25, 0.27, 0.26, 1.37, 0.28, 0.26, 0.27, 0.26, 0.26, 0.21, 0.26, 1.35, 
0.28, 0.26, 0.25, 0.26, 0.21, 0.26, 0.25, 1.36, 0.27, 0.13, 0.26, 0.25, 
0.26, 0.25, 0.25, 1.36, 0.28, 0.26, 0.22, 0.26, 0.24, 0.26, 0.26, 1.39, 
0.28, 0.26, 0.26, 0.26, 0.21, 0.26, 1.35, 0.28, 0.26, 0.25, 0.26, 0.26, 
0.25, 1.38, 0.28, 0.27, 0.26, 0.22, 0.28, 0.25, 1.33, 0.36, 0.26, 0.25, 
0.26, 0.26, 0.26, 1.37, 0.27, 0.26, 0.26, 0.21, 0.23, 0.28, 1.35, 0.23, 
0.21, 0.32, 0.24, 1.38, 0.29, 0.43, 0.24, 0.22, 0.22, 0.27, 1.24, 0.27, 
0.26, 0.26, 0.26, 0.27, 0.22, 1.36, 0.27, 0.26, 0.26, 0.26, 0.26, 0.26, 
1.37, 0.28, 0.26, 0.26, 0.26, 0.26, 0.28, 1.35, 0.27, 0.26, 0.26, 0.26, 
0.26, 0.26, 1.48, 0.25, 0.27, 0.26, 0.13, 0.25, 0.22, 1.41, 0.28, 0.27, 
0.26, 0.26, 0.26, 0.25, 1.39, 0.28, 0.26, 0.26, 0.25, 0.26, 0.26, 1.35, 
0.28, 0.27, 0.26, 0.26, 0.26, 0.26, 1.39, 0.24,

This is the way I understand what is happening.  In execution without 
the wait, there can be some delays in any bit of execution due to other 
tasks getting a share of the time and due to small interrupt routines 
doing I/O.  However, when a task asks the OS to wait, the OS takes this 
as a good time to give control to another task.  Because of this, the 
OS is more likely to give some processor time to another task during a 
wait than during the execution of a handler with an "if" command.  In 
some programming environments, programmers take advantage of this and 
use the equivalent of "wait 0" as meaning "yield", that is, "this is a 
good time to let some other task have a turn at the processor".

There can be lots of tasks that need time.  Other programs can impact 
this.  Activity on the network can also.  And, perhaps, delayed I/O 
related to your screen drawing may be going on.

I think it would be acceptable and commendable programming for you to 
make your own wait.  It doesn't even have to be portable among 
platforms and processor speeds.  In addition to the two conditions you 
have in your example you might insert a middle case that loops looking 
at the milliseconds or the long seconds.  Also, based on your results, 
you might want to include all values less than 1.2 in your 
return-right-away case.  You may also want to subtract an offset for 
the other two cases.  The loop or the wait would use the adjusted value 
to make up for the handler overhead.

By the way, it takes about .015 ms overhead on my computer to get the 
long seconds.  I often measure this in timing routines and subtract it 
out of the timed results.  As you can see, on my computer this is 
small.  However, it might not be so small on yours.  Get the offset by 
measuring as you would with nothing between the lines getting the long 
seconds.  Assuming your computer gets sub ms resolution on long seconds.

Now.  Given all that.  I'm getting longer times than I did last night.  
Also, they seem long.  My computer has dual 1.25 processors.  I would 
have expected a faster time even if the OS forces switch consideration 
every time.  Even so, I think the base time of about 230 microseconds 
is consistent with the Revolution product.

So, if you pencil in a note about the implied yield on the page on 
"wait" in your Transcript Dictionary, then the behavior you see is 
reasonable and expected.  Based on that, making a special wait handler 
for short waits is quite reasonable.

I hope this doesn't include any bunny trails like the one I went down 
before--being distracted by the first wait taking longer.  With the 
errant BlueTooth implant, the temporal lobe surgery and the drugs they 
gave me in prison, I get a little distracted at times and--in in the 
case of your previous mail--I read the email you wrote in some other 
universe, Jim, and missed the example you had written in this one.  I 
hope the quoted handler above matches the one you sent in this universe.

Dar Scott






More information about the use-livecode mailing list