On this page:
6.1 Time Queries
current-ticks
current-ticks-ns
current-time-ns
6.2 Delays
delay!
delay-ns!
delay-precise!
6.3 Performance Counter
performance-counter
performance-frequency
6.4 Timing Utilities
with-timing
6.5 Time Unit Constants
NS_  PER_  SECOND
NS_  PER_  MS
NS_  PER_  US
MS_  PER_  SECOND
9.0.0.11

6 Timer Functions🔗ℹ

This section covers timing and delay functions for animation and frame timing.

6.1 Time Queries🔗ℹ

Returns the number of milliseconds since SDL was initialized. Use this for timing and animation.

Returns the number of nanoseconds since SDL was initialized. More precise than current-ticks but still based on SDL’s internal timer.

Returns high-precision time in nanoseconds using the performance counter. More accurate than current-ticks-ns for profiling and benchmarking.

6.2 Delays🔗ℹ

procedure

(delay! ms)  void?

  ms : real?
Pauses execution for ms milliseconds.

Uses Racket’s sleep internally, which cooperates with the Racket thread scheduler and allows async FFI callbacks to run.

procedure

(delay-ns! ns)  void?

  ns : real?
Pauses execution for ns nanoseconds.

Like delay! but accepts nanoseconds instead of milliseconds.

procedure

(delay-precise! ns)  void?

  ns : exact-nonnegative-integer?
Delays for ns nanoseconds with busy-waiting for precision.

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.

Returns the raw performance counter value.

Values are only meaningful relative to each other. Use performance-frequency to convert to time units.

Returns the performance counter frequency (counts per second).

Use this to convert performance counter differences to time:
(define start (performance-counter))
;; ... work ...
(define end (performance-counter))
(define elapsed-seconds
  (/ (- end start) (performance-frequency)))

6.4 Timing Utilities🔗ℹ

syntax

(with-timing body ...)

Executes body and returns two values: the result of body and the elapsed time in nanoseconds.

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

Nanoseconds per second.

value

NS_PER_MS : exact-positive-integer? = 1000000

Nanoseconds per millisecond.

Nanoseconds per microsecond.

Milliseconds per second.