ucoreTimer
High-precision timing and delay operations.
Functions
| Function | Returns | Description |
|---|---|---|
now() |
float | Current monotonic time in milliseconds |
sleep(ms) |
int | Pause execution for specified milliseconds |
now()
Returns the current monotonic time in milliseconds as a float. Useful for measuring elapsed time.
var start = ucoreTimer.now();
// Do some work
var sum = 0;
for (var i = 0; i < 100000; i = i + 1) {
sum = sum + i;
}
var elapsed = ucoreTimer.now() - start;
print("Elapsed: " + elapsed + " ms");
sleep(ms)
Pauses execution for the specified number of milliseconds.
print("Starting...");
ucoreTimer.sleep(1000); // Wait 1 second
print("1 second later!");
ucoreTimer.sleep(500); // Wait 500ms
print("Done!");
Benchmarking Pattern
function benchmark(name, iterations) {
var t0 = ucoreTimer.now();
var x = 0;
for (var i = 0; i < iterations; i = i + 1) {
x = x + 1;
}
var t1 = ucoreTimer.now();
var ms = t1 - t0;
var opsPerSec = iterations / (ms / 1000);
print(name + ": " + opsPerSec + " ops/sec");
}
benchmark("Loop", 1000000);
Example
./bin/unnarize examples/corelib/timer/demo.unna