Module clock
The clock
module returns time values derived from the Posix / C
CLOCK_GETTIME function or equivalent. Most functions in the module return a
number of seconds; functions whose names end in “64” return a 64-bit number of
nanoseconds.
Below is a list of all clock
functions.
Name | Use |
---|---|
clock.time() clock.realtime() |
Get the wall clock time in seconds |
clock.time64() clock.realtime64() |
Get the wall clock time in nanoseconds |
clock.monotonic() | Get the monotonic time in seconds |
clock.monotonic64() | Get the monotonic time in nanoseconds |
clock.proc() | Get the processor time in seconds |
clock.proc64() | Get the processor time in nanoseconds |
clock.thread() | Get the thread time in seconds |
clock.thread64() | Get the thread time in nanoseconds |
clock.bench() | Measure the time a function takes within a processor |
-
clock.
time
()¶ -
clock.
time64
()¶ -
clock.
realtime
()¶ -
clock.
realtime64
()¶ The wall clock time. Derived from C function
clock_gettime(CLOCK_REALTIME)
.Return: seconds or nanoseconds since epoch (1970-01-01 00:00:00), adjusted. Rtype: number or cdata (ctype<int64_t>) Example:
-- This will print an approximate number of years since 1970. clock = require('clock') print(clock.time() / (365*24*60*60))
See also fiber.time64 and the standard Lua function os.clock.
-
clock.
monotonic
()¶ -
clock.
monotonic64
()¶ The monotonic time. Derived from C function
clock_gettime(CLOCK_MONOTONIC)
. Monotonic time is similar to wall clock time but is not affected by changes to or from daylight saving time, or by changes done by a user. This is the best function to use with benchmarks that need to calculate elapsed time.Return: seconds or nanoseconds since the last time that the computer was booted. Rtype: number or cdata (ctype<int64_t>) Example:
-- This will print nanoseconds since the start. clock = require('clock') print(clock.monotonic64())
-
clock.
proc
()¶ -
clock.
proc64
()¶ The processor time. Derived from C function
clock_gettime(CLOCK_PROCESS_CPUTIME_ID)
. This is the best function to use with benchmarks that need to calculate how much time has been spent within a CPU.Return: seconds or nanoseconds since processor start. Rtype: number or cdata (ctype<int64_t>) Example:
-- This will print nanoseconds in the CPU since the start. clock = require('clock') print(clock.proc64())
-
clock.
thread
()¶ -
clock.
thread64
()¶ The thread time. Derived from C function
clock_gettime(CLOCK_THREAD_CPUTIME_ID)
. This is the best function to use with benchmarks that need to calculate how much time has been spent within a thread within a CPU.Return: seconds or nanoseconds since the transaction processor thread started. Rtype: number or cdata (ctype<int64_t>) Example:
-- This will print seconds in the thread since the start. clock = require('clock') print(clock.thread64())
-
clock.
bench
(function[, ...])¶ The time that a function takes within a processor. This function uses
clock.proc()
, therefore it calculates elapsed CPU time. Therefore it is not useful for showing actual elapsed time.Parameters: - function (
function
) – function or function reference - ... – whatever values are required by the function.
Return: table. first element – seconds of CPU time, second element – whatever the function returns.
Example:
-- Benchmark a function which sleeps 10 seconds. -- NB: bench() will not calculate sleep time. -- So the returned value will be {a number less than 10, 88}. clock = require('clock') fiber = require('fiber') function f(param) fiber.sleep(param) return 88 end clock.bench(f, 10)
- function (