Skip to content

Commit 988cd86

Browse files
committed
Improve benchmark output formatting and adjusted benchmark iterations to hopefully be more fair to older CPUs
1 parent cd7ca01 commit 988cd86

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

bench.lua

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,20 @@ function SystemFamily()
2121
end
2222

2323
function BenchmarkStart(name, iterations)
24-
io.write("Benchmarking " .. name .. " (" .. iterations .. " iterations)...\t")
24+
io.write(string.format("%-9s", name) .. "\t" .. string.format("%10s", iterations) .. "\t")
2525
io.flush()
2626
return os.clock()
2727
end
2828

2929
function BenchmarkEnd(start_time)
30-
io.write(string.format("%010.6f", os.clock() - start_time) .. " seconds\n")
30+
local t = os.clock() - start_time
31+
local m, s = math.floor(t / 60), (t % 60)
32+
io.write(string.format("%03d:%010.7f\n", m, s))
3133
end
3234

3335
local function benchmark_pi()
34-
local it, pi, si = 100000, 3, 1
35-
local bm = BenchmarkStart("Pi ", it)
36+
local it, pi, si = 20000, 3, 1
37+
local bm = BenchmarkStart("Nilakantha Pi", it)
3638

3739
-- Calculate using Nilakantha series
3840
for i = 2, it * 2, 2 do
@@ -52,8 +54,8 @@ local function benchmark_gcd()
5254
return a
5355
end
5456

55-
local it, r = 100000, 0
56-
local bm = BenchmarkStart("GCD", it)
57+
local it, r = 5000, 0
58+
local bm = BenchmarkStart("Common Divisor", it)
5759

5860
for i = 1, it do
5961
local x = i
@@ -66,7 +68,7 @@ end
6668

6769
local function benchmark_mul()
6870
local r, it = 1, 100000
69-
local bm = BenchmarkStart("Mul", it)
71+
local bm = BenchmarkStart("Multiplication", it)
7072

7173
for i = 1, it do
7274
r = (r * i) % 1000000007 -- Keep the result small to avoid overflow
@@ -77,7 +79,7 @@ end
7779

7880
local function benchmark_div()
7981
local it, result = 100000, 1
80-
local bm = BenchmarkStart("Div", it)
82+
local bm = BenchmarkStart("Division", it)
8183

8284
it = it + 1
8385
for i = 2, it do
@@ -89,7 +91,7 @@ end
8991

9092
local function benchmark_add()
9193
local r, it = 1, 100000
92-
local bm = BenchmarkStart("Add", it)
94+
local bm = BenchmarkStart("Addition", it)
9395

9496
for i = 1, it do
9597
r = r + i
@@ -100,7 +102,7 @@ end
100102

101103
local function benchmark_sub()
102104
local r, it = 1, 100000
103-
local bm = BenchmarkStart("Sub", it)
105+
local bm = BenchmarkStart("Subtraction", it)
104106

105107
for i = it, 1, -1 do
106108
r = r - i
@@ -111,7 +113,7 @@ end
111113

112114
local function benchmark_array()
113115
local it = 1000
114-
local bm = BenchmarkStart("Array", it)
116+
local bm = BenchmarkStart("Array Loop", it)
115117

116118
local array = {}
117119
for i = 1, it do
@@ -137,7 +139,7 @@ print("Memory (KB):", collectgarbage("count"))
137139
print("Minimum Int:", math.mininteger or "Unknown")
138140
print("Maximum Int:", math.maxinteger or "Unknown")
139141
print()
140-
142+
print("Benchmark", "Iterations", "Time (min:sec.ms)")
141143
-- Run the benchmarks
142144

143145
bm = os.clock()
@@ -154,4 +156,5 @@ benchmark_array()
154156

155157
print()
156158
print("Total memory usage (KB):", collectgarbage("count"))
157-
print("Total benchmark seconds:", string.format("%010.6f", os.clock() - bm))
159+
io.write("Total benchmark seconds:\t")
160+
BenchmarkEnd(bm)

0 commit comments

Comments
 (0)