@@ -21,18 +21,20 @@ function SystemFamily()
2121end
2222
2323function 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 ()
2727end
2828
2929function 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 ))
3133end
3234
3335local 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
6668
6769local 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
7779
7880local 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
8991
9092local 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
100102
101103local 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
111113
112114local 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"))
137139print (" Minimum Int:" , math.mininteger or " Unknown" )
138140print (" Maximum Int:" , math.maxinteger or " Unknown" )
139141print ()
140-
142+ print ( " Benchmark " , " Iterations " , " Time (min:sec.ms) " )
141143-- Run the benchmarks
142144
143145bm = os.clock ()
@@ -154,4 +156,5 @@ benchmark_array()
154156
155157print ()
156158print (" 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