-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.lua
More file actions
207 lines (187 loc) · 4.8 KB
/
Copy pathbenchmark.lua
File metadata and controls
207 lines (187 loc) · 4.8 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
local opts = {
test_standard = true,
test_string_get_set = true;
test_table = true,
test_map_array = true,
test_sort = true,
}
local loop_count = select(1,...) or (10^7) -- 10^8 单个耗时在1秒以内。太小了。测试不出什么。
if (select(2,...) == 'all') then
for key, value in pairs(opts) do
opts[key] = true
end
end
-- http://lua-users.org/wiki/ObjectBenchmarkTests
-- Benchmarking support.
do
local function runbenchmark(name, code, count, ob)
local f = load([[
local count,ob = ...
local clock = os.clock
local start = clock()
for i=1,count do ]] .. code .. [[ end
return clock() - start
]])
io.write(string.format("%-8.6g| %s\n",f(count, ob), name))
end
local nameof = {}
local codeof = {}
local tests = {}
function addbenchmark(name, code, ob)
nameof[ob] = name
codeof[ob] = code
tests[#tests+1] = ob
end
function clearbenchmark()
tests = {}
nameof = {}
codeof = {}
end
function runbenchmarks(count)
for _,ob in ipairs(tests) do
runbenchmark(nameof[ob], codeof[ob], count, ob)
end
end
end
function makeob1()
local self = {data = 0}
function self:test() self.data = self.data + 1 end
return self
end
addbenchmark("Standard (solid)", "ob:test()", makeob1())
local ob2mt = {}
ob2mt.__index = ob2mt
function ob2mt:test() self.data = self.data + 1 end
function makeob2()
return setmetatable({data = 0}, ob2mt)
end
addbenchmark("Standard (metatable)", "ob:test()", makeob2())
function makeob3()
local self = {data = 0};
function self.test() self.data = self.data + 1 end
return self
end
addbenchmark("Object using closures (PiL 16.4)", "ob.test()", makeob3())
function makeob4()
local public = {}
local data = 0
function public.test() data = data + 1 end
function public.getdata() return data end
function public.setdata(d) data = d end
return public
end
addbenchmark("Object using closures (noself)", "ob.test()", makeob4())
addbenchmark("Local Variable", "ob = ob + 1", 0)
if opts.test_standard then
runbenchmarks(loop_count)
end
clearbenchmark()
addbenchmark("Direct Access", "ob.data = ob.data + 1", makeob1())
if opts.test_string_get_set then
runbenchmarks(loop_count)
end
function make_ob_table()
local arr = {
a1=1,
a2=2,
a3=3,
a4=4,
}
return {
init = function ()
arr = {
a1=1,
a2=2,
a3=3,
a4=4,
}
end,
forin_and_set = function ()
for k,v in pairs(arr) do
arr[k] = v+1
end
end,
}
end
clearbenchmark()
addbenchmark("table init", "ob.init()", make_ob_table())
addbenchmark("table forin_and_set", "ob.forin_and_set()", make_ob_table())
if opts.test_table then
runbenchmarks(loop_count/8)
end
function make_ob_arr(size)
size = size or 4
local arr
if table.newarray then
arr = table.newarray()
else
arr = {}
end
for i = 1, size do
arr[i] = i
end
return {
init = function ()
if table.newarray then
arr = table.newarray()
else
arr = {}
end
-- arr = []
-- table.resize(arr, size)
for i = 1, size do
arr[i] = i
end
end,
reset = function ()
for i = 1, size do
arr[i] = i
end
end,
forin = function ()
for k,v in pairs(arr) do
-- do nothing
end
end
}
end
function test_array_size(arr_size)
local loop_count = loop_count//arr_size
if loop_count <= 0 then
return
end
clearbenchmark()
addbenchmark("array["..arr_size.."] init", "ob.init()", make_ob_arr(arr_size))
addbenchmark("array["..arr_size.."] reset", "ob.reset()", make_ob_arr(arr_size))
addbenchmark("array["..arr_size.."] forin", "ob.forin()", make_ob_arr(arr_size))
runbenchmarks(loop_count)
end
if opts.test_map_array then
test_array_size(2)
test_array_size(4)
test_array_size(20)
test_array_size(200)
test_array_size(2000)
end
clearbenchmark()
function make_ob_sort(size)
size = size or 4
local arr = {}
for i = 1, size do
arr[i] = i * 23142543 % 12353
end
return {
sort = function ()
if table.stable_sort then
table.stable_sort(arr)
else
table.sort(arr)
end
end,
}
end
if opts.test_sort then
local arr_size = 1000
addbenchmark("array["..arr_size.."] sort", "ob.sort()", make_ob_sort(arr_size))
runbenchmarks(loop_count // arr_size // 10)
end