Skip to content

Commit 2102d3f

Browse files
committed
Refactor benchmarks and improve code clarity
Streamlined variable names for consistency and brevity, removed redundant comments, and revised print statements.
1 parent c1197b2 commit 2102d3f

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

bench.lua

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,16 @@ local function benchmark_pi()
3636
local it, pi, si = 20000, 3, 1
3737
local bm = BenchmarkStart("Nilakantha Pi", it)
3838

39-
-- Calculate using Nilakantha series
4039
for i = 2, it * 2, 2 do
4140
pi = pi + si * (4 / (i * (i + 1) * (i + 2)))
42-
si = -si -- Alternate the sign for each term
41+
si = -si
4342
end
4443

4544
BenchmarkEnd(bm)
4645
end
4746

4847
local function benchmark_gcd()
49-
-- Function to compute the greatest common divisor
50-
local function gcd(a, b)
48+
local function gcd(a, b) -- Function to compute the greatest common divisor
5149
while b ~= 0 do
5250
a, b = b, a % b
5351
end
@@ -67,7 +65,7 @@ local function benchmark_gcd()
6765
end
6866

6967
local function benchmark_mul()
70-
local r, it = 1, 100000
68+
local it, r = 100000, 1
7169
local bm = BenchmarkStart("Multiplication", it)
7270

7371
for _ = 1, it do
@@ -78,12 +76,12 @@ local function benchmark_mul()
7876
end
7977

8078
local function benchmark_div()
81-
local it, result = 100000, 1
79+
local it, r = 100000, 1
8280
local bm = BenchmarkStart("Division", it)
8381

8482
it = it + 1
8583
for i = 2, it do
86-
result = result / i
84+
r = r / i
8785
end
8886

8987
BenchmarkEnd(bm)
@@ -126,18 +124,18 @@ local function benchmark_array()
126124
local it = 1000
127125
local bm = BenchmarkStart("Array Loop", it)
128126

129-
local array = {}
127+
local a = {}
130128
for i = 1, it do
131-
array[i] = i % 10
129+
a[i] = i % 10
132130
end
133131

134132
for i = 1, it do
135-
array[i] = array[i] * 2
133+
a[i] = a[i] * 2
136134
end
137135

138-
local sum = 0
136+
local s = 0
139137
for i = 1, it do
140-
sum = sum + array[i]
138+
s = s + a[i]
141139
end
142140

143141
BenchmarkEnd(bm)
@@ -149,11 +147,10 @@ print("System Family:", SystemFamily())
149147
print("Memory Used:", string.format("%.3fkB", collectgarbage("count")))
150148
print("Minimum Int:", math.mininteger or "Unknown")
151149
print("Maximum Int:", math.maxinteger or "Unknown")
152-
print()
153-
print("Benchmark", "Iterations", "Time (min:sec.ms)")
150+
print("\nBenchmark", "Iterations", "Time (min:sec.ms)")
154151

155152
-- Run the benchmarks
156-
bm = os.clock()
153+
bm = os.clock() -- Start the total time clock
157154
benchmark_add()
158155
benchmark_flt()
159156
benchmark_sub()
@@ -162,7 +159,9 @@ benchmark_div()
162159
benchmark_pi()
163160
benchmark_gcd()
164161
benchmark_array()
165-
print(string.rep('_', 49))
162+
163+
-- Print memory and total time summary at the end of the table
164+
print(string.rep('_', 49)) -- Print a horizontal line to separate results from final summary
166165
io.write("Total:\t\t" .. string.format("%8.3fkB", collectgarbage("count")) .. "\t")
167166
BenchmarkEnd(bm)
168167
print("Press Enter to Exit...") io.read() os.exit()

readme.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ else
1818
end
1919

2020
print('Good ' .. timeOfDay .. ' from ' .. _VERSION .. '.') -- Print a greeting
21-
io.read() -- Wait for Enter to be pressed
22-
print('Have a good ' .. timeOfDay .. '.') -- This message will
23-
os.exit() -- Exit script. Will also exit a interactive shell
21+
print("Press Enter to Exit...")
22+
io.read() -- Wait for Enter to be pressed before proceeding
23+
os.exit() -- Exit the script. This function call will also exit an interactive shell

0 commit comments

Comments
 (0)