@@ -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 )
4645end
4746
4847local 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()
6765end
6866
6967local 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()
7876end
7977
8078local 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())
149147print (" Memory Used:" , string.format (" %.3fkB" , collectgarbage (" count" )))
150148print (" Minimum Int:" , math.mininteger or " Unknown" )
151149print (" Maximum Int:" , math.maxinteger or " Unknown" )
152- print ()
153- print (" Benchmark" , " Iterations" , " Time (min:sec.ms)" )
150+ print (" \n Benchmark" , " Iterations" , " Time (min:sec.ms)" )
154151
155152-- Run the benchmarks
156- bm = os.clock ()
153+ bm = os.clock () -- Start the total time clock
157154benchmark_add ()
158155benchmark_flt ()
159156benchmark_sub ()
@@ -162,7 +159,9 @@ benchmark_div()
162159benchmark_pi ()
163160benchmark_gcd ()
164161benchmark_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
166165io.write (" Total:\t\t " .. string.format (" %8.3fkB" , collectgarbage (" count" )) .. " \t " )
167166BenchmarkEnd (bm )
168167print (" Press Enter to Exit..." ) io.read () os.exit ()
0 commit comments