0Pricing
Lua Academy · Lesson

Measuring Durations

Time how long code runs.

Measuring Durations is a free Lua Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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)

os.time Is Coarse

os.time counts whole seconds, so it cannot see anything faster than one second. Two quick calls in a row often show a difference of zero.

For short tasks you need a finer clock, which is where os.clock comes in.

local a = os.time()
local b = os.time()
print(b - a)

Meet os.clock

os.clock() returns a number of seconds, but as a decimal with fractions. It measures CPU time used by your program with high resolution.

This makes it ideal for timing fast operations down to fractions of a second.

print(os.clock())

Measuring a Loop

Wrap a loop between two os.clock readings to see how long it ran. The difference is the elapsed time in seconds.

Larger loops take longer, so this is a quick way to compare approaches.

local t1 = os.clock()
local sum = 0
for i = 1, 1000000 do sum = sum + i end
local t2 = os.clock()
print("Took", t2 - t1, "seconds")

Formatting the Result

A long decimal is hard to read, so use string.format to show a tidy number of places.

Here %.3f prints three digits after the point, giving milliseconds-level detail.

local t1 = os.clock()
for i = 1, 500000 do end
local elapsed = os.clock() - t1
print(string.format("%.3f seconds", elapsed))

os.time vs os.clock

Choose the right tool. Use os.time for real-world spans like "how many seconds since the user logged in."

Use os.clock for measuring code performance, since it is precise and tracks CPU time rather than wall-clock time.

A Reusable Timer Function

You can wrap the pattern in a function. It runs a given function, times it, and prints the result.

This keeps your timing logic in one place and easy to reuse.

local function timeIt(fn)
  local s = os.clock()
  fn()
  print(string.format("%.4f s", os.clock() - s))
end
timeIt(function() for i=1,100000 do end end)

Difference Across Hours

For long real-world gaps, os.difftime on two os.time values is clearest. It returns the span in seconds.

Divide by 60 for minutes or 3600 for hours to report it nicely.

local started = os.time({year=2025, month=1, day=1, hour=9})
local ended = os.time({year=2025, month=1, day=1, hour=17})
local hours = os.difftime(ended, started) / 3600
print(hours, "hours")

Showing Minutes and Seconds

To present a duration as minutes and seconds, use integer division and the modulo operator.

// gives whole minutes and % gives the leftover seconds.

local total = 145
local mins = total // 60
local secs = total % 60
print(string.format("%d:%02d", mins, secs))

A Simple Countdown Calc

Combine ideas to report time remaining. Subtract now from a target, then format the seconds into minutes and seconds.

This gives a clean countdown display.

local target = os.time() + 90
local left = os.difftime(target, os.time())
print(string.format("%d:%02d left", left // 60, left % 60))

Quick Check

Pick the best tool for precise timing.

Recap

To measure a duration, read a clock before and after, then subtract. Use os.time for whole-second, real-world spans and os.clock for precise, fractional-second code timing.

Format results with string.format, and turn raw seconds into minutes and seconds using // and %. These tools let you time anything from a loop to a workday.

Frequently asked questions

Is the “Measuring Durations” lesson free?

Yes — the full text of “Measuring Durations” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.

What will I learn in “Measuring Durations”?

Time how long code runs. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Lua Academy?

No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Measuring Durations” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Lua Academy lesson?

Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. os.time and os.date
  2. Formatting Dates
  3. Time Arithmetic
  4. Measuring Durations
← Back to Lua Academy