Created
July 10, 2023 17:58
hook_libs_bench.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- gmod vs srlion vs dash hook libs | |
-- to run this rename srlion & dash hook libs to hook_srlion & hook_dash | |
-- from incredible-gmod.ru with <3 | |
local function sum(tbl) | |
local out = 0 | |
for i = 1, #tbl do | |
out = out + tbl[i] | |
end | |
return out | |
end | |
local function avg(tbl) | |
return sum(tbl) / #tbl | |
end | |
local function median(tbl) | |
table.sort(tbl) | |
return #tbl % 2 == 0 && (tbl[#tbl * .5] + tbl[(#tbl * .5) + 1]) * .5 || tbl[math.ceil(#tbl * .5)] | |
end | |
local clock = os.clock | |
local function bench(func, times) | |
local start = clock() | |
for i = 1, times do | |
func(i) | |
end | |
return clock() - start | |
end | |
local function benchmark(name, func, times, rep) | |
local co = coroutine.running() | |
if co then | |
timer.Simple(0, function() | |
collectgarbage() | |
coroutine.resume(co) | |
end) | |
coroutine.yield() | |
else | |
collectgarbage() | |
end | |
local results = {} | |
for i = 1, rep do | |
results[i] = bench(func, times) | |
end | |
local sum, avg, median = sum(results), avg(results), median(results) | |
MsgC( | |
Color(26, 188, 156), name, ":\n", | |
Color(255, 255, 255), "\tsum = ", Color(52, 152, 219), sum, "\n", | |
Color(255, 255, 255), "\tavg = ", Color(52, 152, 219), avg, "\n", | |
Color(255, 255, 255), "\tmedian = ", Color(52, 152, 219), median, "\n" | |
) | |
return sum, avg, median | |
end | |
print(string.rep(" \n", 10)) | |
local function prepare(lib) | |
for i = 1, 5 do | |
lib.Add("hooklib-bech", "bech-".. i, function() end) | |
end | |
end | |
--coroutine.wrap(function() | |
prepare(hook) | |
local g_sum, g_avg, g_median = benchmark("gmod", function() | |
hook.Run("hooklib-bech", "test") | |
end, 100000, 100) | |
prepare(hook_srlion) | |
local s_sum, s_avg, s_median = benchmark("srlion", function() | |
hook_srlion.Run("hooklib-bech", "test") | |
end, 100000, 100) | |
prepare(hook_dash) | |
local d_sum, d_avg, d_median = benchmark("dash", function() | |
hook_dash.Run("hooklib-bech", "test") | |
end, 100000, 100) | |
prepare(hook_dash2) | |
local d2_sum, d2_avg, d2_median = benchmark("dash2", function() | |
hook_dash2.Run("hooklib-bech", "test") | |
end, 100000, 100) | |
print(string.rep(" \n", 2)) | |
MsgC( | |
Color(26, 188, 156), "srlion diff:\n", | |
Color(255, 255, 255), "\tsum = ", Color(52, 152, 219), (s_sum / g_sum) * 100, "\n", | |
Color(255, 255, 255), "\tavg = ", Color(52, 152, 219), (s_avg / g_avg) * 100, "\n", | |
Color(255, 255, 255), "\tmedian = ", Color(52, 152, 219), (s_median / g_median) * 100, "\n" | |
) | |
MsgC( | |
Color(26, 188, 156), "dash diff:\n", | |
Color(255, 255, 255), "\tsum = ", Color(52, 152, 219), (g_sum / d_sum) * 100, "\n", | |
Color(255, 255, 255), "\tavg = ", Color(52, 152, 219), (g_avg / d_avg) * 100, "\n", | |
Color(255, 255, 255), "\tmedian = ", Color(52, 152, 219), (g_median / d_median) * 100, "\n" | |
) | |
MsgC( | |
Color(26, 188, 156), "dash2 diff:\n", | |
Color(255, 255, 255), "\tsum = ", Color(52, 152, 219), (g_sum / d2_sum) * 100, "\n", | |
Color(255, 255, 255), "\tavg = ", Color(52, 152, 219), (g_avg / d2_avg) * 100, "\n", | |
Color(255, 255, 255), "\tmedian = ", Color(52, 152, 219), (g_median / d2_median) * 100, "\n" | |
) | |
--end)() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment