-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetatest.lua
More file actions
75 lines (64 loc) · 1.28 KB
/
Copy pathmetatest.lua
File metadata and controls
75 lines (64 loc) · 1.28 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
67
68
69
70
71
72
73
74
75
n, m = ...
function standard()
local x = 1
for j = 1, m or 1000 do
for k = 1, 10 do
x = math.sin(math.sin(k)-x)
end
end
return x
end
function withmeta()
local x = 1
for j = 1, m or 1000 do
for k = 1, 10 do
x = (k:sin()-x):sin()
end
end
return x
end
function withmetamethod()
local x = 1
for j = 1, m or 1000 do
for k = 1, 10 do
x = getmetatable(x).__sin(getmetatable(k).__sin(k)-x)
end
end
return x
end
function withcachedmetamethod()
local x = 1
local __sin = getmetatable(x).__sin
for j = 1, m or 1000 do
for k = 1, 10 do
x = __sin(__sin(k)-x)
end
end
return x
end
local os_clock = os.clock
function run(fn)
print(fn)
local f = _G[fn]
local t = 0
for i = 1, 500 do
t = t+f()
end
local time0 = os_clock()
for i = 1, n or 1000 do
t = t+f()
end
local time1 = os_clock()
print(time1-time0, t)
end
run("standard")
debug.setmetatable(0, {
__index = {
sin = math.sin,
},
__sin = math.sin,
})
run("withmeta")
run("withmetamethod")
run("withcachedmetamethod")
run("standard")