Measuring Durations
Time how long code runs.
Timing Your Code
Sometimes you want to know how long a task took: loading a file, running a loop, or building a level. Measuring durations helps you find slow spots and improve them.
Lua gives you two tools for this: os.time for coarse timing and os.clock for fine timing.
The Stopwatch Pattern
The basic idea is simple: record the time before the work, do the work, record the time after, then subtract.
The difference is the elapsed duration. This start-stop pattern is the heart of every timer.
local start = os.time()
-- work happens here
local stop = os.time()
print("Elapsed:", stop - start)All lessons in this course
- os.time and os.date
- Formatting Dates
- Time Arithmetic
- Measuring Durations