local bench = {}
bench.runs = 20
bench.extraRuns = 4
function bench.runCode(f, description)
if callgrind and callgrind("running") then
if collectgarbage then collectgarbage() end
callgrind("zero")
f() callgrind("dump", description)
return
end
local timeTable = {}
for i = 1,bench.runs + bench.extraRuns do
if collectgarbage then
pcall(function()
collectgarbage()
end)
end
local ts0 = os.clock()
local result = f()
local ts1 = os.clock()
if not result then
result = ts1 - ts0
end
table.insert(timeTable, result)
end
table.sort(timeTable)
for i = 1,bench.extraRuns do
table.remove(timeTable, #timeTable)
end
local report = "|><|"..description
for _,v in ipairs(timeTable) do
report = report .. "|><|" .. (v * 1000)
end
report = report .. "||_||"
print(report)
end
function bench.runScriptCodeUnderActor(scriptInstance, f, description)
if scriptInstance:GetActor() then
bench.runCode(f, description)
return true
else
local actor = Instance.new("Actor")
local benchModule = script:Clone()
benchModule.Parent = actor
local actorScript = scriptInstance:Clone()
actorScript.Disabled = false
actorScript.Parent = actor
actor.Parent = workspace
return false
end
end
return bench