Skip to content

Commit 2b2306b

Browse files
committed
Added benchmark date to BENCH.LUA script. The year range starts at 1980 and runs benchmarks exponentially more times with each passing year.
1 parent 2ed9355 commit 2b2306b

File tree

2 files changed

+39
-56
lines changed

2 files changed

+39
-56
lines changed

demo/core/BENCH.LUA

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,76 +7,65 @@ function OS()
77
return os.getenv("SHELL") and "UNIX" or "Unknown"
88
end
99
end
10-
1110
function BS(name, iterations)
12-
io.write(string.format("%-9s", name) .. "\t" .. string.format("%10s", iterations) .. "\t")
13-
io.flush()
14-
return os.clock()
11+
local t = #tostring(iterations) > 10 and string.format("%10e", iterations):gsub("%..-e", "e") or iterations
12+
io.write(string.format("%-9s", name) .. "\t" .. string.format("%10s", t) .. "\t") io.flush() return os.clock()
1513
end
16-
1714
function BE(start_time)
1815
local t = os.clock() - start_time
1916
local m, s = math.floor(t / 60), (t % 60)
2017
io.write(string.format("%7d:%09.6f\n", m, s))
2118
end
22-
2319
local function b_pi()
24-
local it, pi, si = 20000, 3, 1
20+
local it, pi, si = S, 3, 1
2521
local b = BS("Nilakantha Pi", it)
2622
for i = 2, it * 2, 2 do
27-
pi = pi + si * (4 / (i * (i + 1) * (i + 2)))
28-
si = -si
29-
end
30-
BE(b)
23+
pi = pi + si * (4 / (i * (i + 1) * (i + 2))) si = -si
24+
end BE(b)
3125
end
3226
local function b_gcd()
33-
local function gcd(a, b) -- Function to compute the greatest common divisor
34-
while b ~= 0 do a, b = b, a % b end
35-
return a
36-
end
37-
local m, r = 5000, 0
38-
local b = BS("Common Divisor", m)
39-
for i = 1, m do local x = i local y = m - i + 1 r = gcd(x, y) end BE(b)
27+
local function gcd(a, b) while b ~= 0 do a, b = b, a % b end return a end
28+
local r, b = 0, BS("Common Divisor", S)
29+
for i = 1, S do local x = i local y = S - i + 1 r = gcd(x, y) end BE(b)
4030
end
4131
local function b_mul()
42-
local m, r = 100000, 1
43-
local b = BS("Multiplication", m)
44-
for _ = 1, m do r = (r * 1.000000001) end BE(b)
32+
local r, b = 1, BS("Multiplication", B)
33+
for _ = 1, B do r = (r * 1.000000001) end BE(b)
4534
end
4635
local function b_div()
47-
local m, r = 100000, 1
48-
local b = BS("Division", m)
49-
m = m + 1
36+
local m, r = B, 1
37+
local b = BS("Division", m) m = m + 1
5038
for i = 2, m do r = r / i end BE(b)
5139
end
5240
local function b_add()
53-
local r, m = 1, 100000
54-
local b = BS("Addition", m)
55-
for i = 1, m do r = r + i end BE(b)
41+
local r, b = 1, BS("Addition", B)
42+
for i = 1, B do r = r + i end BE(b)
5643
end
5744
local function b_flt()
58-
local r, m = 1.0, 100000
59-
local b = BS("Float Addition", m)
60-
for _ = 1, m do r = r + 0.01 end BE(b)
45+
local r, b = 1.0, BS("Float Addition", B)
46+
for _ = 1, B do r = r + 0.01 end BE(b)
6147
end
6248
local function b_sub()
63-
local r, m = 1, 100000
64-
local b = BS("Subtraction", m)
65-
for i = m, 1, -1 do r = r - i end BE(b)
49+
local r, b = 1, BS("Subtraction", B)
50+
for i = B, 1, -1 do r = r - i end BE(b)
6651
end
6752
local function b_arr()
68-
local m = 1000
69-
local b = BS("Array Loop", m)
70-
local a, s = {}, 0
71-
for i = 1, m do a[i] = i % 10 end
72-
for i = 1, m do a[i] = a[i] * 2 end
73-
for i = 1, m do s = s + a[i] end BE(b)
53+
local b, a, s = BS("Array Loop", S), {}, 0
54+
for i = 1, S do a[i] = i % 10 end
55+
for i = 1, S do a[i] = a[i] * 2 end
56+
for i = 1, S do s = s + a[i] end BE(b)
7457
end
7558
local function L() print(string.rep('_', 49)) end
7659
print("Runtime:", _VERSION)
7760
print("OS Family:", OS())
7861
print("Minimum Int:", math.mininteger or "Unknown")
7962
print("Maximum Int:", math.maxinteger or "Unknown")
63+
M=tonumber(arg[1])
64+
if not M or M < 1980 then
65+
print("\nTo benchmark, run again and specify a year > 1979") os.exit()
66+
end
67+
print("\nRunning benchmark: " .. M) M = (M-1955)/25
68+
B, S = math.floor(500^M), math.floor(100^M)
8069
print("\nBenchmark", "Iterations", "Time (min:sec.ms)")
8170
L()
8271
b = os.clock()

demo/xtra/EXBENCH.LUA

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,36 @@
11
if not LIB then print("Please run bench.lua from the same working directory instead") return end
22

33
local function benchmark_md5sum()
4-
local it, s = 2, pcall(require, "MD5SUM")
5-
local bm = BS("Checksum MD5", it)
6-
local file = io.open(arg[0], "rb")
4+
local s, bm, file = pcall(require, "MD5SUM"), BS("Checksum MD5", T), io.open(arg[0], "rb")
75

86
if not s or not file then
97
if file then file:close() end
108
print(string.format("%17s", "Skipped"))
119
return
1210
end
1311

14-
for _ = 1, it do
15-
md5_file(file)
16-
file:seek("set", 0)
12+
for _ = 1, T do
13+
md5_file(file) file:seek("set", 0)
1714
end
18-
file:close()
19-
BE(bm)
15+
16+
file:close() BE(bm)
2017
end
2118

2219
local function benchmark_sha256()
23-
local it, s = 1, pcall(require, "S256SUM")
24-
local bm = BS("Checksum SHA256", it)
25-
local file = io.open(arg[0], "rb")
20+
local s, bm, file = pcall(require, "S256SUM"), BS("Checksum SHA256", T), io.open(arg[0], "rb")
2621

2722
if not s or not file then
2823
if file then file:close() end
2924
print(string.format("%17s", "Skipped"))
3025
return
3126
end
3227

33-
for _ = 1, it do
34-
sha256_file(file)
35-
file:seek("set", 0)
28+
for _ = 1, T do
29+
sha256_file(file) file:seek("set", 0)
3630
end
37-
file:close()
38-
BE(bm)
39-
end
4031

32+
file:close() BE(bm)
33+
end
34+
T=math.floor(2^M)
4135
benchmark_md5sum()
4236
benchmark_sha256()

0 commit comments

Comments
 (0)