inspect.lua/perf.lua
Enrique García Cota f18082ac47 (perf) introduce perf test harness and refactor (#54)
Made a lot of backwards-compatible changes and simplifications to the code that I had wanted to do for a lot of time, while also keeping an eye in performance. The new changes improved overall performance on my limited, dev-machine-only, luajit-only tests.

Before:

```
$ luajit perf.lua
nil,string,empty,sequence,record,hybrid,recursive,meta,process,complex,big
0.054246,0.140177,0.149759,0.323062,0.441312,0.607064,0.296031,0.458631,0.105668,1.717528,2.047272
```

After:

```
$ luajit perf.lua
nil,string,empty,sequence,record,hybrid,recursive,meta,process,complex,big
0.036804,0.188984,0.099349,0.247425,0.342079,0.452339,0.203801,0.288931,0.169462,1.010763,1.616462
```
2022-03-18 13:44:36 +01:00

113 lines
1.7 KiB
Lua

local inspect = require 'inspect'
local skip_headers = ...
local N=100000
local results = {}
local time = function(name, n, f)
local clock = os.clock
collectgarbage()
collectgarbage()
collectgarbage()
local startTime = clock()
for i=0,n do f() end
local duration = clock() - startTime
results[#results + 1] = { name, duration }
end
-------------------
time('nil', N, function()
inspect(nil)
end)
time('string', N, function()
inspect("hello")
end)
local e={}
time('empty', N, function()
inspect(e)
end)
local seq={1,2,3,4}
time('sequence', N, function()
inspect(seq)
end)
local record={a=1, b=2, c=3}
time('record', N, function()
inspect(record)
end)
local hybrid={1, 2, 3, a=1, b=2, c=3}
time('hybrid', N, function()
inspect(hybrid)
end)
local recursive = {}
recursive.x = recursive
time('recursive', N, function()
inspect(recursive)
end)
local with_meta=setmetatable({},
{ __tostring = function() return "s" end })
time('meta', N, function()
inspect(with_meta)
end)
local process_options = {
process = function(i,p) return "p" end
}
time('process', N, function()
inspect(seq, process_options)
end)
local complex = {
a = 1,
true,
print,
[print] = print,
[{}] = { {}, 3, b = {x = 42} }
}
complex.x = complex
setmetatable(complex, complex)
time('complex', N, function()
inspect(complex)
end)
local big = {}
for i = 1,1000 do
big[i] = i
end
for i = 1,1000 do
big["a" .. i] = 1
end
time('big', N/100, function()
inspect(big)
end)
------
if not skip_headers then
for i,r in ipairs(results) do
if i > 1 then io.write(",") end
io.write(r[1])
end
io.write("\n")
end
for i,r in ipairs(results) do
if i > 1 then io.write(",") end
io.write(r[2])
end
io.write("\n")