-- bench_object.lua — object/string/call benchmark (Lua) local iterations = 200000 local clock = os.clock local function bench_record_create() local r for i = 0, iterations - 1 do r = {x = i, y = i + 1, z = i + 2} end return r.z end local function bench_prop_read() local obj = {x = 10, y = 20, z = 30, w = 40} local s = 0 for i = 0, iterations - 1 do s = s + obj.x + obj.y + obj.z + obj.w end return s end local function bench_dynamic_prop() local obj = {a = 1, b = 2, c = 3, d = 4, e = 5} local keys = {"a", "b", "c", "d", "e"} local s = 0 for i = 0, iterations - 1 do for j = 1, 5 do s = s + obj[keys[j]] end end return s end local function bench_string_concat() local parts = {} local n = 10000 for i = 1, n do parts[i] = "x" end local s = table.concat(parts) return #s end local function bench_interpolation() local s = "" local n = 50000 for i = 0, n - 1 do s = string.format("item_%d", i) end return s end local function make_point(x, y) return { x = x, y = y, sum = function(self) return self.x + self.y end } end local function bench_method_call() local p = make_point(3, 4) local s = 0 for i = 0, iterations - 1 do s = s + p.sum(p) end return s end local function fib(n) if n <= 1 then return n end return fib(n - 1) + fib(n - 2) end local function bench_fncall() local s = 0 for i = 0, 19 do s = s + fib(25) end return s end local function run(name, fn) local start = clock() local result = fn() local elapsed = (clock() - start) * 1000 print(string.format(" %s: %.2f ms (result: %s)", name, elapsed, tostring(result))) end print("=== Object / String / Call Benchmark ===") print(string.format(" iterations: %d", iterations)) run("record_create ", bench_record_create) run("prop_read ", bench_prop_read) run("dynamic_prop ", bench_dynamic_prop) run("string_concat ", bench_string_concat) run("interpolation ", bench_interpolation) run("method_call ", bench_method_call) run("fncall_fib25 ", bench_fncall)