-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbench_closures.lua
More file actions
66 lines (58 loc) · 1.56 KB
/
Copy pathbench_closures.lua
File metadata and controls
66 lines (58 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
-- Benchmark: Closures and Upvalues
local iterations = 1000000
print("=== Closures & Upvalues Benchmark ===")
print("Iterations:", iterations)
-- Closure creation
local start = os.clock()
for i = 1, iterations do
local x = i
local f = function() return x end
end
local elapsed = os.clock() - start
print(string.format("Closure creation: %.3f seconds (%.2f M ops/sec)", elapsed, iterations / elapsed / 1000000))
-- Closure with upvalue read
local counter = 0
local function make_counter()
local count = 0
return function()
count = count + 1
return count
end
end
local inc = make_counter()
start = os.clock()
for i = 1, iterations do
inc()
end
elapsed = os.clock() - start
print(string.format("Upvalue read/write: %.3f seconds (%.2f M ops/sec)", elapsed, iterations / elapsed / 1000000))
-- Multiple upvalues
local function make_adder(a, b, c)
return function(x)
return a + b + c + x
end
end
local adder = make_adder(1, 2, 3)
start = os.clock()
local sum = 0
for i = 1, iterations do
sum = adder(i)
end
elapsed = os.clock() - start
print(string.format("Multiple upvalues: %.3f seconds (%.2f M ops/sec)", elapsed, iterations / elapsed / 1000000))
-- Nested closures
local function outer(x)
return function(y)
return function(z)
return x + y + z
end
end
end
local f1 = outer(1)
local f2 = f1(2)
start = os.clock()
for i = 1, iterations do
sum = f2(i)
end
elapsed = os.clock() - start
print(string.format("Nested closures: %.3f seconds (%.2f M ops/sec)", elapsed, iterations / elapsed / 1000000))