|
1 | | -print("PROMETHEUS Benchmark") |
2 | | -print("Based On IronBrew Benchmark") |
3 | | -local Iterations = 100000 |
4 | | -print("Iterations: " .. tostring(Iterations)) |
5 | | - |
6 | | -print("CLOSURE testing.") |
7 | | -local Start = os.clock() |
8 | | -local TStart = Start |
9 | | -for _ = 1, Iterations do |
10 | | - (function() |
11 | | - if not true then |
12 | | - print("Hey gamer.") |
| 1 | +local benchmarks = {} |
| 2 | + |
| 3 | +local function add(name, iters, fn) |
| 4 | + benchmarks[#benchmarks+1] = { name = name, iters = iters, fn = fn } |
| 5 | +end |
| 6 | + |
| 7 | +-- Arithmetic / numeric loop |
| 8 | +add("arithmetic loop", 500000, function(iters) |
| 9 | + local x = 0 |
| 10 | + for i = 1, iters do |
| 11 | + x = x + i |
| 12 | + end |
| 13 | + return x |
| 14 | +end) |
| 15 | + |
| 16 | +-- Function call overhead |
| 17 | +add("function calls", 400000, function(iters) |
| 18 | + local function f(a,b,c) return a + b * c end |
| 19 | + local s = 0 |
| 20 | + for i = 1, iters do |
| 21 | + s = s + f(i, 2, 3) |
| 22 | + end |
| 23 | + return s |
| 24 | +end) |
| 25 | + |
| 26 | +-- Table creation & insertion |
| 27 | +add("table create/insert", 400000, function(iters) |
| 28 | + local last |
| 29 | + for i = 1, iters do |
| 30 | + local t = {i, i*2, i*3} |
| 31 | + t[i % 3 + 1] = i |
| 32 | + last = t |
| 33 | + end |
| 34 | + return last |
| 35 | +end) |
| 36 | + |
| 37 | +-- Table iteration (pairs) over fixed table |
| 38 | +add("table iteration", 2000, function(iters) |
| 39 | + local t = {} |
| 40 | + for i = 1, 1000 do t[i] = i end |
| 41 | + local s = 0 |
| 42 | + for _ = 1, iters do |
| 43 | + for k,v in pairs(t) do |
| 44 | + s = s + v - k |
13 | 45 | end |
14 | | - end)() |
| 46 | + end |
| 47 | + return s |
| 48 | +end) |
| 49 | + |
| 50 | +-- String concatenation (intentional repeated ..) |
| 51 | +add("string concat", 4000, function(iters) |
| 52 | + local s = "" |
| 53 | + for i = 1, iters do |
| 54 | + s = s .. "a" |
| 55 | + end |
| 56 | + return #s |
| 57 | +end) |
| 58 | + |
| 59 | +-- Closure creation (allocating many small closures) |
| 60 | +add("closure creation", 300000, function(iters) |
| 61 | + local acc = 0 |
| 62 | + local list = {} |
| 63 | + for i = 1, iters do |
| 64 | + local f = function() return i end |
| 65 | + acc = acc + f() |
| 66 | + list[i % 16] = f -- keep a few alive |
| 67 | + end |
| 68 | + return acc |
| 69 | +end) |
| 70 | + |
| 71 | +-- Metatable __index access |
| 72 | +add("metatable index", 1000000, function(iters) |
| 73 | + local base = { value = 123 } |
| 74 | + local proxy = {} |
| 75 | + setmetatable(proxy, { __index = base }) |
| 76 | + local s = 0 |
| 77 | + for i = 1, iters do |
| 78 | + s = s + proxy.value |
| 79 | + end |
| 80 | + return s |
| 81 | +end) |
| 82 | + |
| 83 | +local function run(b) |
| 84 | + collectgarbage() |
| 85 | + local start = os.clock() |
| 86 | + b.fn(b.iters) |
| 87 | + local elapsed = os.clock() - start |
| 88 | + return elapsed |
| 89 | +end |
| 90 | + |
| 91 | +-- Column widths |
| 92 | +local name_w = 4 |
| 93 | +local iter_w = 10 |
| 94 | +local time_w = 8 |
| 95 | +for i = 1, #benchmarks do |
| 96 | + local b = benchmarks[i] |
| 97 | + if #b.name > name_w then name_w = #b.name end |
| 98 | + local ilen = string.len(tostring(b.iters)) |
| 99 | + if ilen > iter_w then iter_w = ilen end |
15 | 100 | end |
16 | | -print("Time:", os.clock() - Start .. "s") |
17 | 101 |
|
18 | | -print("SETTABLE testing.") |
19 | | -Start = os.clock() |
20 | | -local T = {} |
21 | | -for Idx = 1, Iterations do |
22 | | - T[tostring(Idx)] = "EPIC GAMER " .. tostring(Idx) |
| 102 | +local function pad(s, w) |
| 103 | + local l = #s |
| 104 | + if l < w then |
| 105 | + return s .. string.rep(" ", w - l) |
| 106 | + end |
| 107 | + return s |
23 | 108 | end |
24 | 109 |
|
25 | | -print("Time:", os.clock() - Start .. "s") |
| 110 | +print("prometheus benchmark") |
| 111 | +print(pad("name", name_w) .. " " .. pad("iterations", iter_w) .. " time(s)") |
26 | 112 |
|
27 | | -print("GETTABLE testing.") |
28 | | -Start = os.clock() |
29 | | -for Idx = 1, Iterations do |
30 | | - T[1] = T[tostring(Idx)] |
| 113 | +local total = 0; |
| 114 | +for i = 1, #benchmarks do |
| 115 | + local b = benchmarks[i] |
| 116 | + local t = run(b) |
| 117 | + total = total + t; |
| 118 | + local time_s = string.format("%.6f", t) |
| 119 | + if #time_s > time_w then time_w = #time_s end |
| 120 | + print(pad(b.name, name_w) .. " " .. pad(tostring(b.iters), iter_w) .. " " .. time_s) |
31 | 121 | end |
32 | 122 |
|
33 | | -print("Time:", os.clock() - Start .. "s") |
34 | | -print("Total Time:", os.clock() - TStart .. "s") |
| 123 | +print(pad("total", name_w) .. " " .. pad("", iter_w) .. " " .. string.format("%.6f", total)) |
0 commit comments