Script:
--?module-ancestors {script}
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SimpleSignal = require(ReplicatedStorage.SimpleSignal)
local Benchmark = require(script.Benchmark)
local SignalX = require(ReplicatedStorage.SignalX)
local BadSivnal = require(ReplicatedStorage.BadSivnal)
local C_AMOUNT = 1000
local FIRES = 10
local s = SimpleSignal.new()
local bads = BadSivnal:new()
for i = 1, C_AMOUNT do
s:Connect(function(b)
local a = i * b
task.wait()
end)
bads:Push(true, function(b)
local a = i * b
task.wait()
end)
end
local badsTotal = {}
local simpleTotal = {}
print("waiting...")
task.wait(5)
for i = 1, 20 do
do
local total = Benchmark.findavg(FIRES, "total", 10, 1, function()
bads:Fire(2)
end)
table.insert(badsTotal, total)
end
do
local total = Benchmark.findavg(FIRES, "total", 10, 1, function()
s:Fire(2)
end)
table.insert(simpleTotal, total)
end
end
print("simple fire: ", Benchmark.mean(simpleTotal, FIRES))
print("bads fire: ", Benchmark.mean(badsTotal, FIRES))
Benchmark:
-- Benchmark utility module
local function table_sum(t)
local sum = 0
for _, v in t do
sum += v
end
return sum
end
local function table_maxv(t)
local max = -math.huge
for _, v in t do
if v > max then
max = v
end
end
return max
end
local function table_minv(t)
local min = math.huge
for _, v in t do
if v < min then
min = v
end
end
return min
end
local function table_mode(t)
local counts = {}
local maxCount = 0
local modeValue = 0
for _, value in t do
counts[value] = (counts[value] or 0) + 1
if counts[value] > maxCount then
maxCount = counts[value]
modeValue = value
end
end
return modeValue -- Return only the first most popular mode
end
local function dround(n: number, r: number)
local p = 10^r
return math.floor(n * p) / p
end
-- Returns the time it took <code>testfn</code> to run after the initial call.
local function benchmark_start<A..., R...>(testfn: (A...) -> R..., ...: A...): (number, R...)
local start = os.clock()
local result = {testfn(...)}
return os.clock() - start, unpack(result)
end
--[[
Calls <code>testfn</code> <code>repeats</code> times and returns <code>avtype</code> elapsed time (truncated to <code>decimals</code> decimal places).
]]
local function benchmark_findavg(repeats: number, avtype: "mean"|"max"|"min"|"median"|"mode"|"total", decimals: number, benchInterval: number, testfn: () -> ()): (number, {number})
local times = {}
local total = 0
for i = 1, repeats do
local t = benchmark_start(testfn)
total += t
table.insert(times, t)
if i % benchInterval == 0 then
task.wait()
end
end
table.sort(times)
if avtype == "max" then
return dround(table_maxv(times), decimals), times
elseif avtype == "min" then
return dround(table_minv(times), decimals), times
elseif avtype == "median" then
return dround(times[#times//2], decimals), times
elseif avtype == "mean" then
return dround(table_sum(times)/repeats, decimals), times
elseif avtype == "mode" then
return dround(table_mode(times), decimals), times
elseif avtype == "total" then
return dround(total, decimals), times
else
error(`Unknown avtype {avtype}`)
end
end
local function stdDeviation(times, mean)
local sum = 0
for _, v in times do
sum += (v - mean)^2
end
return math.sqrt(sum/#times)
end
local Benchmark = {
findavg = benchmark_findavg,
start = benchmark_start,
stdDeviation = stdDeviation,
mean = function(t, repeats)
return table_sum(t)/repeats
end,
--[[<docstring>
hi
]]
cool_var = 10
}
return Benchmark
For other people who are still skeptical of either one or both of our claims you can test it yourself too :V