Skip to content

Commit cd7ca01

Browse files
committed
Refactor Lua scripts to be executable on UNIX. Refactored bench.lua to be more compact.
1 parent 7a4bdf7 commit cd7ca01

File tree

2 files changed

+49
-80
lines changed

2 files changed

+49
-80
lines changed

bench.lua

Lines changed: 49 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,30 @@ function SystemFamily()
2020
end
2121
end
2222

23-
local function benchmark_pi()
24-
local iterations = 100000
25-
local pi = 3
26-
local sign = 1
23+
function BenchmarkStart(name, iterations)
24+
io.write("Benchmarking " .. name .. " (" .. iterations .. " iterations)...\t")
25+
io.flush()
26+
return os.clock()
27+
end
2728

28-
io.write("Benchmarking Pi (" .. iterations .. " iterations)...\t")
29+
function BenchmarkEnd(start_time)
30+
io.write(string.format("%010.6f", os.clock() - start_time) .. " seconds\n")
31+
end
2932

30-
local start_time = os.clock()
33+
local function benchmark_pi()
34+
local it, pi, si = 100000, 3, 1
35+
local bm = BenchmarkStart("Pi ", it)
3136

3237
-- Calculate using Nilakantha series
33-
for i = 2, iterations * 2, 2 do
34-
pi = pi + sign * (4 / (i * (i + 1) * (i + 2)))
35-
sign = -sign -- Alternate the sign for each term
38+
for i = 2, it * 2, 2 do
39+
pi = pi + si * (4 / (i * (i + 1) * (i + 2)))
40+
si = -si -- Alternate the sign for each term
3641
end
3742

38-
local end_time = os.clock()
39-
40-
io.write(string.format("%.6f", end_time - start_time) .. " seconds\n")
43+
BenchmarkEnd(bm)
4144
end
4245

4346
local function benchmark_gcd()
44-
4547
-- Function to compute the greatest common divisor
4648
local function gcd(a, b)
4749
while b ~= 0 do
@@ -50,113 +52,82 @@ local function benchmark_gcd()
5052
return a
5153
end
5254

53-
local iterations = 100000
54-
local result = 0
55+
local it, r = 100000, 0
56+
local bm = BenchmarkStart("GCD", it)
5557

56-
io.write("Benchmarking GCD (" .. iterations .. " iterations)...\t")
57-
58-
local start_time = os.clock()
59-
60-
for i = 1, iterations do
58+
for i = 1, it do
6159
local x = i
62-
local y = iterations - i + 1
63-
result = gcd(x, y)
60+
local y = it - i + 1
61+
r = gcd(x, y)
6462
end
6563

66-
local end_time = os.clock()
67-
68-
io.write(string.format("%.6f", end_time - start_time) .. " seconds\n")
64+
BenchmarkEnd(bm)
6965
end
7066

7167
local function benchmark_mul()
72-
local iterations = 100000
68+
local r, it = 1, 100000
69+
local bm = BenchmarkStart("Mul", it)
7370

74-
io.write("Benchmarking Mul (" .. iterations .. " iterations)...\t")
75-
76-
local start_time = os.clock()
77-
local result = 1
78-
for i = 1, iterations do
79-
result = (result * i) % 1000000007 -- Keep the result small to avoid overflow
71+
for i = 1, it do
72+
r = (r * i) % 1000000007 -- Keep the result small to avoid overflow
8073
end
8174

82-
local end_time = os.clock()
83-
84-
io.write(string.format("%.6f", end_time - start_time) .. " seconds\n")
75+
BenchmarkEnd(bm)
8576
end
8677

8778
local function benchmark_div()
88-
local iterations = 100000
79+
local it, result = 100000, 1
80+
local bm = BenchmarkStart("Div", it)
8981

90-
io.write("Benchmarking Div (" .. iterations .. " iterations)...\t")
91-
iterations = iterations + 1
92-
93-
local start_time = os.clock()
94-
local result = 1
95-
for i = 2, iterations do
82+
it = it + 1
83+
for i = 2, it do
9684
result = result / i
9785
end
9886

99-
local end_time = os.clock()
100-
101-
io.write(string.format("%.6f", end_time - start_time) .. " seconds\n")
87+
BenchmarkEnd(bm)
10288
end
10389

10490
local function benchmark_add()
105-
local iterations = 100000
106-
107-
io.write("Benchmarking Add (" .. iterations .. " iterations)...\t")
91+
local r, it = 1, 100000
92+
local bm = BenchmarkStart("Add", it)
10893

109-
local start_time = os.clock()
110-
local result = 1
111-
for i = 1, iterations do
112-
result = result + i
94+
for i = 1, it do
95+
r = r + i
11396
end
11497

115-
local end_time = os.clock()
116-
117-
io.write(string.format("%.6f", end_time - start_time) .. " seconds\n")
98+
BenchmarkEnd(bm)
11899
end
119100

120101
local function benchmark_sub()
121-
local iterations = 100000
122-
123-
io.write("Benchmarking Sub (" .. iterations .. " iterations)...\t")
102+
local r, it = 1, 100000
103+
local bm = BenchmarkStart("Sub", it)
124104

125-
local start_time = os.clock()
126-
local result = 1
127-
for i = iterations, 1, -1 do
128-
result = result - i
105+
for i = it, 1, -1 do
106+
r = r - i
129107
end
130108

131-
local end_time = os.clock()
132-
133-
io.write(string.format("%.6f", end_time - start_time) .. " seconds\n")
109+
BenchmarkEnd(bm)
134110
end
135111

136112
local function benchmark_array()
137-
local elements = 1000
138-
139-
io.write("Benchmarking Array (" .. elements .. " iterations)...\t")
140-
141-
local start_time = os.clock()
113+
local it = 1000
114+
local bm = BenchmarkStart("Array", it)
142115

143116
local array = {}
144-
for i = 1, elements do
117+
for i = 1, it do
145118
array[i] = i % 10
146119
end
147120

148-
for i = 1, elements do
121+
for i = 1, it do
149122
array[i] = array[i] * 2
150123
end
151124

152125
local sum = 0
153-
for i = 1, elements do
126+
for i = 1, it do
154127
sum = sum + array[i]
155128
end
156129

157-
local end_time = os.clock()
158-
159-
io.write(string.format("%.6f", end_time - start_time) .. " seconds\n")
130+
BenchmarkEnd(bm)
160131
end
161132

162133
-- Some information
@@ -169,7 +140,7 @@ print()
169140

170141
-- Run the benchmarks
171142

172-
start_time = os.clock()
143+
bm = os.clock()
173144

174145
benchmark_add()
175146
benchmark_sub()
@@ -181,8 +152,6 @@ benchmark_gcd()
181152

182153
benchmark_array()
183154

184-
end_time = os.clock()
185-
186155
print()
187156
print("Total memory usage (KB):", collectgarbage("count"))
188-
print("Total benchmark time: ", string.format("%.6f", end_time - start_time))
157+
print("Total benchmark seconds:", string.format("%010.6f", os.clock() - bm))

readme.lua

100644100755
File mode changed.

0 commit comments

Comments
 (0)