This repository was archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathBenchmarker.lua
More file actions
232 lines (200 loc) · 4.75 KB
/
Benchmarker.lua
File metadata and controls
232 lines (200 loc) · 4.75 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
-- This version may give different results!
-- The original version can be found here: https://pastebin.com/raw/0pVCCryf
----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
local bit = getrenv().bit32
local osclock = os.clock
local benchmarks = {
{
Name = "Array3d",
Function = function()
local function ArraySet(self, x, y, z, p)
local nx = self.nx
assert(x >= 0 and x < nx, "x outside PA")
assert(y >= 0 and y < nx, "y outside PA")
assert(z >= 0 and z < nx, "z outside PA")
local pos = (z * self.ny + y) * nx + x
local image = self.image
if self.packed then
local maxv = self.max_voltage
if p > maxv then
self.max_voltage = p * 2
end
local oldp = image[pos] or 0
if oldp > maxv then
p += maxv * 2
end
image[pos] = p
else
image[pos] = p
end
self.changed = true
self.changed_recently = true
end
local function ArrayPoints(self)
local y, z = 0, 0
return function(self, x)
x += 1
if x >= self.nx then
x = 0
y += 1
if y >= self.ny then
y = 0
z += 1
end
if z >= self.nz then
return
end
end
return x, y, z
end, self, 0
end
local function ArrayNew(nx, ny, nz, packed)
return {
nx = nx,
ny = ny,
nz = nz,
packed = packed,
max_voltage = 0,
changed = false,
changed_recently = false,
image = {},
set = ArraySet,
points = ArrayPoints
}
end
local packed = true
local function RunIterator(dim)
local array = ArrayNew(dim, dim, dim, packed)
for x, y, z in array:points() do
array:set(x, y, z, x * x)
end
assert(array.image[dim ^ 3 - 1] == (dim - 1) ^ 2)
end
return RunIterator(200)
end
},
{
Name = "binarytrees",
Function = function()
local mindepth = 4
local maxdepth = 12
local function BottomUpTree(item, depth)
if depth > 0 then
local i = item + item
depth -= 1
local left, right = BottomUpTree(i - 1, depth), BottomUpTree(i, depth)
return {item, left, right}
else
return {item}
end
end
local function ItemCheck(tree)
if #tree == 3 then
return tree[1] + ItemCheck(tree[2]) - ItemCheck(tree[3])
else
return tree[1]
end
end
local function InnerIterator(min, max)
local check = 0
local stretchdepth = maxdepth + 1
local stretchtree = BottomUpTree(0, stretchdepth)
check += ItemCheck(stretchtree)
local tree = BottomUpTree(0, maxdepth)
for depth = mindepth, maxdepth, 2 do
local iterations = 2 ^ (maxdepth - depth + mindepth)
for i = 1, iterations do
check += ItemCheck(BottomUpTree(1, depth)) + ItemCheck(BottomUpTree(-1, depth))
end
end
check += ItemCheck(tree)
end
local function RunIterator(n)
for i = 1, n do
InnerIterator(mindepth, maxdepth)
end
end
return RunIterator(3)
end
},
{
Name = "coroutines",
Function = function()
local size = 503
local threads = {}
local create, resume, yield = coroutine.create, coroutine.resume, coroutine.yield
local id = 1
local token = 0
local ok
local body = function(token)
while true do
token = yield(token + 1)
end
end
for id = 1, size do
threads[id] = create(body)
end
local function RunIterator(n)
id = 1
token = 0
repeat
if id == size then
id = 1
else
id += 1
end
ok, token = resume(threads[id], token)
until token == n
end
return RunIterator(3300000)
end
},
{
Name = "euler",
Function = function()
local bnot, bor, band = bit.bnot, bit.bor, bit.band
local shl, shr = bit.lshift, bit.rshift
local function RunIterator(n)
local cache, m, _n = {1}, 1, 1
for i = 2, n do
local j = i
for len = 1, 1000000000 do
j = bor(band(shr(j, 1), band(j, 1) - 1), band(shl(j, 1) + j + 1, bnot(band(j, 1) - 1)))
if cache then
local x = cache[j]
if x then
j = x + len
break
end
elseif j == 1 then
j = len + 1
break
end
end
if cache then
cache[i] = j
end
if j > m then
m, _n = j, i
end
end
assert(m > 1 and _n > 1)
end
return RunIterator(3500000)
end
}
}
warn("[Robot Benchmarker]: Your game will freeze temporarily.")
task.wait(2)
warn("[Robot Benchmarker]: Running benchmarks...")
for _,v in next, benchmarks do
local start = osclock()
print(" [+]: Running benchmark '"..v.Name.."'...")
local a = pcall(v.Function)
if not a then
print(" [+]: Benchmark failed, is your exploit shit?")
else
print(" [+]: Benchmark finished in "..(osclock() - start).."\n")
end
end
warn("[Robot Benchmarker]: Finished all benchmarks.")