Small cross-platform monotonic time helper for Lua.
Use it to measure elapsed time, like a stopwatch. It is not for real date or clock time.
Works with Lua 5.1, 5.2, 5.3, 5.4, 5.5, and LuaJIT.
local mtime = require "mtime"
local started_at = mtime.now()
os.execute("sleep 1")
local ended_at = mtime.now()
local elapsed_ns = ended_at - started_at
local elapsed_ms = math.floor((elapsed_ns / 1000000) + 0.5)
local elapsed_s = elapsed_ns / 1000000000
print("Timing Report")
print("Start :", started_at .. " ns")
print("End :", ended_at .. " ns")
print("Elapsed ns :", elapsed_ns)
print("Elapsed ms :", elapsed_ms .. " ms")
print("Elapsed s :", elapsed_s .. " s")
print("Summary :", "operation completed in " .. elapsed_ms .. " ms")
- benchmarks
- test duration reporting in a test library or runner
- profiling a slow function or code path
- checking how long a loop, query, or file operation took
- timing retries, backoff, or timeout windows
- measuring game loop or frame step durations
- collecting performance metrics in apps or tools
luarocks install mtime