|
1 | 1 | #!/usr/bin/env lua |
2 | 2 |
|
3 | 3 | function OS() |
4 | | - if os.getenv("COMSPEC") then |
5 | | - return os.getenv("OS") and "NT" or "DOS" |
6 | | - else |
7 | | - return os.getenv("SHELL") and "UNIX" or "Unknown" |
8 | | - end |
9 | | -end |
10 | | -function BS(name, iterations) |
11 | | - local t = #tostring(iterations) > 10 and string.format("%10e", iterations):gsub("%..-e", "e") or iterations |
12 | | - io.write(string.format("%-9s", name) .. "\t" .. string.format("%10s", t) .. "\t") io.flush() return os.clock() |
13 | | -end |
14 | | -function BE(start_time) |
15 | | - local t = os.clock() - start_time |
16 | | - local m, s = math.floor(t / 60), (t % 60) |
17 | | - io.write(string.format("%7d:%09.6f\n", m, s)) |
18 | | -end |
| 4 | + if os.getenv("COMSPEC")then return os.getenv("OS")and"NT"or"DOS" |
| 5 | + else return os.getenv("SHELL")and"UNIX"or"Unknown"end end |
| 6 | +function BS(name, it) |
| 7 | + local t=#tostring(it)>10 and string.format("%10e",it):gsub("%..-e","e")or it |
| 8 | + io.write(string.format("%-9s",name).."\t"..string.format("%10s",t).."\t")io.flush()return os.clock()end |
| 9 | +function BE(st) |
| 10 | + local t=os.clock()-st |
| 11 | + local m,s=math.floor(t/60),t%60 |
| 12 | + io.write(string.format("%7d:%09.6f\n",m,s))end |
19 | 13 | local function b_pi() |
20 | | - local it, pi, si = S, 3, 1 |
21 | | - local b = BS("Nilakantha Pi", it) |
22 | | - for i = 2, it * 2, 2 do |
23 | | - pi = pi + si * (4 / (i * (i + 1) * (i + 2))) si = -si |
24 | | - end BE(b) |
25 | | -end |
| 14 | + local it,pi,si=S,3,1 |
| 15 | + local b=BS("Nilakantha Pi",it) |
| 16 | + for i=2,it*2,2 do pi=pi+si*(4/(i*(i+1)*(i+2)))si=-si end BE(b)end |
26 | 17 | local function b_gcd() |
27 | | - local function gcd(a, b) while b ~= 0 do a, b = b, a % b end return a end |
28 | | - local r, b = 0, BS("Common Divisor", S) |
29 | | - for i = 1, S do local x = i local y = S - i + 1 r = gcd(x, y) end BE(b) |
30 | | -end |
| 18 | + local function gcd(a,b)while b~=0 do a,b=b,a%b end return a end |
| 19 | + local r,b=0,BS("Common Divisor",S) |
| 20 | + for i=1,S do local x,y=i,S-i+1 r=gcd(x,y)end BE(b)end |
31 | 21 | local function b_mul() |
32 | | - local r, b = 1, BS("Multiplication", B) |
33 | | - for _ = 1, B do r = (r * 1.000000001) end BE(b) |
34 | | -end |
| 22 | + local r,b=1,BS("Multiplication",B) |
| 23 | + for _=1,B do r=(r*1.000000001)end BE(b)end |
35 | 24 | local function b_div() |
36 | | - local m, r = B, 1 |
37 | | - local b = BS("Division", m) m = m + 1 |
38 | | - for i = 2, m do r = r / i end BE(b) |
39 | | -end |
| 25 | + local m,r=B,1 |
| 26 | + local b=BS("Division",m)m=m+1 |
| 27 | + for i=2,m do r=r/i end BE(b)end |
40 | 28 | local function b_add() |
41 | | - local r, b = 1, BS("Addition", B) |
42 | | - for i = 1, B do r = r + i end BE(b) |
43 | | -end |
| 29 | + local r,b=1,BS("Addition",B) |
| 30 | + for i=1,B do r=r+i end BE(b)end |
44 | 31 | local function b_flt() |
45 | | - local r, b = 1.0, BS("Float Addition", B) |
46 | | - for _ = 1, B do r = r + 0.01 end BE(b) |
47 | | -end |
| 32 | + local r,b=1.0,BS("Float Addition",B) |
| 33 | + for _=1,B do r=r+0.01 end BE(b)end |
48 | 34 | local function b_sub() |
49 | | - local r, b = 1, BS("Subtraction", B) |
50 | | - for i = B, 1, -1 do r = r - i end BE(b) |
51 | | -end |
| 35 | + local r,b=1,BS("Subtraction",B) |
| 36 | + for i=B,1,-1 do r=r-i end BE(b)end |
52 | 37 | local function b_arr() |
53 | | - local b, a, s = BS("Array Loop", S), {}, 0 |
54 | | - for i = 1, S do a[i] = i % 10 end |
55 | | - for i = 1, S do a[i] = a[i] * 2 end |
56 | | - for i = 1, S do s = s + a[i] end BE(b) |
57 | | -end |
58 | | -local function L() print(string.rep('_', 49)) end |
59 | | -print("Runtime:", _VERSION) |
60 | | -print("OS Family:", OS()) |
61 | | -print("Minimum Int:", math.mininteger or "Unknown") |
62 | | -print("Maximum Int:", math.maxinteger or "Unknown") |
63 | | -M=tonumber(arg[1]) |
64 | | -if not M or M < 1980 then |
65 | | - print("\nTo benchmark, run again and specify a year > 1979") os.exit() |
66 | | -end |
67 | | -print("\nRunning benchmark: " .. M) M = (M-1955)/25 |
68 | | -B, S = math.floor(500^M), math.floor(100^M) |
69 | | -print("\nBenchmark", "Iterations", "Time (min:sec.ms)") |
70 | | -L() |
71 | | -b = os.clock() |
72 | | -b_add() b_flt() b_sub() b_mul() b_div() b_pi() b_gcd() b_arr() |
73 | | -LIB = true |
74 | | -pcall(require, "EXBENCH") |
75 | | -L() |
76 | | -io.write("Total:\t\t" .. string.format("%8.3fkB", collectgarbage("count")) .. "\t") BE(b) |
77 | | -print("Press Enter to Exit...") io.read() os.exit() |
| 38 | + local b,a,s=BS("Array Loop",S),{},0 |
| 39 | + for i=1,S do a[i]=i%10 end |
| 40 | + for i=1,S do a[i]=a[i]*2 end |
| 41 | + for i=1,S do s=s+a[i]end BE(b)end |
| 42 | +local function L()print(string.rep('_',49))end |
| 43 | +print("Runtime:",_VERSION)print("OS Family:",OS()) |
| 44 | +print("Minimum Int:",math.mininteger or"Unknown") |
| 45 | +print("Maximum Int:",math.maxinteger or"Unknown") |
| 46 | +M=tonumber(arg[1])if not M or M<1980 then print("\nTo benchmark,run again and specify a year > 1979")os.exit()end |
| 47 | +print("\nRunning benchmark: "..M)M=(M-1955)/25 |
| 48 | +B,S=math.floor(500^M),math.floor(100^M) |
| 49 | +print("\nBenchmark","Iterations","Time (min:sec.ms)")L() |
| 50 | +b=os.clock()b_add()b_flt()b_sub()b_mul()b_div()b_pi()b_gcd()b_arr() |
| 51 | +LIB=true package.path=package.path..";?.LUA" pcall(require,"EXBENCH")L() |
| 52 | +io.write("Total:\t\t"..string.format("%8.3fkB",collectgarbage("count")).."\t")BE(b) |
| 53 | +print("Press Enter to Exit...")io.read()os.exit() |
0 commit comments