Subject: Re: dalay-time function
From: (Rob Warnock)
Date: 27 Apr 2001 03:36:10 GMT
Newsgroups: comp.lang.scheme
Message-ID: <9capfa$3grtg$>
Matthias Felleisen <> wrote:
| > Where can I find a version of Scheme with the function delay-time ?
| > ...DrScheme... this function is supposed to let the system waiting
| for 1/60 s, ex : (delay-time 120)  let the system wait 2s...
| Try
|   ; sleep : Number  *->  Void
|   ; to sleep for a specified number of seconds (see Help Desk)

To expand on Matthias's answer a little bit...

You can trivially create your "delay-time" function in MzScheme/DrScheme
like this:

	(define (delay-time delta)
	  (sleep (/ delta 60)))

However, you need to be aware that delaying in increments of exactly
1/60 second may not be possible on some operating systems (at least,
not without more privileges than an ordinary user). Usually there is
a way for a normal user to delay for "one scheduling tick", but these
days that's more likely to be 1/100 sec than 1/60. If your application
is mostly making references to fairly large numbers of sixtieths, then
in practice the above may well be "good enough".

Indeed, MzScheme tries pretty hard to make that work well, but how
well it succeeds depends on the underlying O/S. As it says in the docs
[note the last line!!]:

    (sleep [x]) causes the current thread to sleep for at least x seconds,
    where x is a non-negative real number. ... The value of x can be
    non-integral to request a sleep duration to any precision, but the
    precision of the actual sleep time is unspecified. 

Under Unix, MzScheme uses the "select()" system call to do the delaying,
and is this limited to whatever temporal quanta the O/S provides to
users. Try the following on your system:

    > (define (test-sleep quantum total-period)
        (let ((n (/ total-period quantum)))
	  (do ((i 0 (+ i 1)))
	      ((>= i n))
	    (display #\*)
	    (sleep quantum))
    > (test-sleep 0.1 3)
    > (test-sleep 0.1 3)
    > (test-sleep 0.05 3)
    > (test-sleep 0.05 3)

See how close the total time for each run approximates the second argument.
E.g., on my SGI O2, the first set of parameters seems to run almost exactly
3 seconds, while the second takes somewhere between 3 and 3-1/2 seconds.
[But that's just "eyeballing" it...]  *Some* of the increase in total time
with finer granularity will be due simply to executing the inner loop more
times, but the rest will probably be due to a mismatch between the amount
of delay you request and the granularity available from the underlying
operating system to MzScheme [which "rounds up" before calling "select()"].


Rob Warnock, 31-2-510
SGI Network Engineering		<URL:>
1600 Amphitheatre Pkwy.		Phone: 650-933-1673
Mountain View, CA  94043	PP-ASEL-IA