Home  Contents

os.ticks

OS Core4 Lua Commands

SYNOPSIS

val = os.ticks()

DESCRIPTION

The system provides a timer that counts up monotonically at 1000Hz.

RETURN VALUE

The current value of the ticks timer as an unsigned 32-bit value.

NOTES

Since a 32-bit value might run over after some time, care is needed when checking for an elapsed time.

EXAMPLE

Determine how long some code runs:

>  >  > 
t0 = os.ticks() someExpensiveFunction() print(string.format("Running took %dms", os.ticks() - t0))

Since a 32-bit value might run over after some time, care is needed when checking for an elapsed time.

Good code, by using a subtraction, any roll over will cancel itself:

Good >       >       > 
t0 = os.ticks() someExpensiveFunction() if (os.ticks() - t0 > 1000) then print("Took more than one second") end

The following code will fail if the ticks counter runs over between the two calls:

Bad  >       >       > 
t0 = os.ticks() someExpensiveFunction() if (os.ticks() > t0 + 1000) then print("Took more than one second") end