6 Timer Functions
This section covers timing and delay functions for animation and frame timing.
6.1 Time Queries
procedure
procedure
procedure
6.2 Delays
Uses Racket’s sleep internally, which cooperates with the Racket thread scheduler and allows async FFI callbacks to run.
Like delay! but accepts nanoseconds instead of milliseconds.
procedure
(delay-precise! ns) → void?
ns : exact-nonnegative-integer?
More CPU-intensive but more accurate than delay-ns!. Good for frame timing where precise delays are needed.
6.3 Performance Counter
For advanced timing needs, these functions provide direct access to the high-resolution performance counter.
procedure
Values are only meaningful relative to each other. Use performance-frequency to convert to time units.
procedure
(define start (performance-counter)) ;; ... work ... (define end (performance-counter)) (define elapsed-seconds (/ (- end start) (performance-frequency)))
6.4 Timing Utilities
syntax
(with-timing body ...)
Uses the high-precision performance counter for accurate measurement.
(define-values (result elapsed-ns) (with-timing (some-expensive-computation))) (printf "Took ~a ns~n" elapsed-ns)
6.5 Time Unit Constants
These constants are provided for convenience when working with time values:
value
NS_PER_SECOND : exact-positive-integer? = 1000000000
value
NS_PER_MS : exact-positive-integer? = 1000000
value
NS_PER_US : exact-positive-integer? = 1000
value
MS_PER_SECOND : exact-positive-integer? = 1000