Skip to main content

Standard Library: Timing & Memory

Timing builtins are globals. There is no import std.time.


Clocks and sleep​

FunctionDescription
clockMs()Monotonic milliseconds
timeNow()Monotonic nanoseconds
sleep(ms) / wait(ms) / timeSleep(ms)Suspend the current thread
const start = timeNow();
sleep(10);
print(timeNow() - start);

Measuring work​

measure(fn) runs a thunk and returns elapsed time. profileLog(label, value) emits a timing record when profiling is enabled. Keep timing off hot paths unless you are profiling.

const elapsed = measure(fn (): void {
var total = 0;
loop (var i = 0; i < 100000; i = i + 1) {
total = total + i;
}
});
profileLog("sum", elapsed);

Garbage collection diagnostics​

FunctionDescription
collectGarbage()Request a collection
gcStats()Heap diagnostic string