1+ #! /usr/bin/env lua
2+
3+ function SystemFamily ()
4+ local env = os.getenv (" COMSPEC" )
5+
6+ if env then
7+ env = os.getenv (" OS" )
8+ if env then
9+ return " NT"
10+ else
11+ return " DOS"
12+ end
13+ else
14+ env = os.getenv (" SHELL" )
15+ if env then
16+ return " UNIX"
17+ else
18+ return " Unknown"
19+ end
20+ end
21+ end
22+
23+ local function benchmark_pi ()
24+ local iterations = 100000
25+ local pi = 3
26+ local sign = 1
27+
28+ io.write (" Benchmarking Pi (" .. iterations .. " iterations)...\t " )
29+
30+ local start_time = os.clock ()
31+
32+ -- 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
36+ end
37+
38+ local end_time = os.clock ()
39+
40+ io.write (string.format (" %.6f" , end_time - start_time ) .. " seconds\n " )
41+ end
42+
43+ local function benchmark_gcd ()
44+
45+ -- Function to compute the greatest common divisor
46+ local function gcd (a , b )
47+ while b ~= 0 do
48+ a , b = b , a % b
49+ end
50+ return a
51+ end
52+
53+ local iterations = 100000
54+ local result = 0
55+
56+ io.write (" Benchmarking GCD (" .. iterations .. " iterations)...\t " )
57+
58+ local start_time = os.clock ()
59+
60+ for i = 1 , iterations do
61+ local x = i
62+ local y = iterations - i + 1
63+ result = gcd (x , y )
64+ end
65+
66+ local end_time = os.clock ()
67+
68+ io.write (string.format (" %.6f" , end_time - start_time ) .. " seconds\n " )
69+ end
70+
71+ local function benchmark_mul ()
72+ local iterations = 100000
73+
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
80+ end
81+
82+ local end_time = os.clock ()
83+
84+ io.write (string.format (" %.6f" , end_time - start_time ) .. " seconds\n " )
85+ end
86+
87+ local function benchmark_div ()
88+ local iterations = 100000
89+
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
96+ result = result / i -- Add 10 to avoid divide-by-zero
97+ end
98+
99+ local end_time = os.clock ()
100+
101+ io.write (string.format (" %.6f" , end_time - start_time ) .. " seconds\n " )
102+ end
103+
104+ local function benchmark_add ()
105+ local iterations = 100000
106+
107+ io.write (" Benchmarking Add (" .. iterations .. " iterations)...\t " )
108+
109+ local start_time = os.clock ()
110+ local result = 1
111+ for i = 1 , iterations do
112+ result = result + i
113+ end
114+
115+ local end_time = os.clock ()
116+
117+ io.write (string.format (" %.6f" , end_time - start_time ) .. " seconds\n " )
118+ end
119+
120+ local function benchmark_sub ()
121+ local iterations = 100000
122+
123+ io.write (" Benchmarking Sub (" .. iterations .. " iterations)...\t " )
124+
125+ local start_time = os.clock ()
126+ local result = 1
127+ for i = iterations , 1 , - 1 do
128+ result = result - i
129+ end
130+
131+ local end_time = os.clock ()
132+
133+ io.write (string.format (" %.6f" , end_time - start_time ) .. " seconds\n " )
134+ end
135+
136+ local function benchmark_array ()
137+ local elements = 1000 -- Total number of array elements (adjust for intensity)
138+
139+ io.write (" Benchmarking Array (" .. elements .. " iterations)...\t " )
140+
141+ local start_time = os.clock ()
142+
143+ local array = {}
144+ for i = 1 , elements do
145+ array [i ] = i % 10 -- Arbitrary initialization
146+ end
147+
148+ for i = 1 , elements do
149+ array [i ] = array [i ] * 2 -- Double every element
150+ end
151+
152+ local sum = 0
153+ for i = 1 , elements do
154+ sum = sum + array [i ]
155+ end
156+
157+ local end_time = os.clock ()
158+
159+ io.write (string.format (" %.6f" , end_time - start_time ) .. " seconds\n " )
160+ end
161+
162+ -- Some information
163+ print (" Interpreter:" , _VERSION )
164+ print (" System Family:" , SystemFamily ())
165+ print (" Memory (KB):" , collectgarbage (" count" ))
166+ print (" Minimum Int:" , math.mininteger or " Unknown" )
167+ print (" Maximum Int:" , math.maxinteger or " Unknown" )
168+ print ()
169+
170+ -- Run the benchmarks
171+
172+ start_time = os.clock ()
173+
174+ benchmark_add ()
175+ benchmark_sub ()
176+ benchmark_mul ()
177+ benchmark_div ()
178+
179+ benchmark_pi ()
180+ benchmark_gcd ()
181+
182+ benchmark_array ()
183+
184+ end_time = os.clock ()
185+
186+ print ()
187+ print (" Total memory usage (KB):" , collectgarbage (" count" ))
188+ print (" Total benchmark time: " , string.format (" %.6f" , end_time - start_time ))
0 commit comments