Last active
May 25, 2023 10:56
-
-
Save Beherith/87ff4a5612b548e6d2f8817126f9508a to your computer and use it in GitHub Desktop.
test engine call perf
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
function widget:GetInfo() | |
return { | |
name = "Test Engine Call Perf", | |
desc = "see how long it takes to run large loops of enigne calls", | |
author = "beherith", | |
date = "20211114", | |
license = "GNU GPL, v2 or later", | |
layer = 10, | |
enabled = false -- loaded by default? | |
} | |
end | |
local unitID | |
function widget:Initialize() | |
allunits = Spring.GetAllUnits() | |
unitID = allunits[1] | |
end | |
local spGetUnitPosition = Spring.GetUnitPosition | |
local engine = false | |
local numloops = 1000000 | |
local spGetTimerMicros = Spring.GetTimerMicros | |
local spGetGameFrame = Spring.GetGameFrame | |
local function luavsengine() | |
local x,y,z | |
local x = 1 | |
local y = 1 | |
local z = 1 | |
if engine then -- these are tuned to be almost exactly equivalent in perf! | |
for i=1, numloops do | |
x,y,z = spGetUnitPosition(unitID) | |
end | |
else | |
for i =1, numloops do | |
x = x + 1 + x * 0.1 - x * 0.1 | |
y = y + 1 + y * 0.1 - y * 0.1 | |
z = z + 1 + z * 0.1 - z * 0.1 | |
end | |
end | |
end | |
local arraytable = {} | |
local hashtable = {} | |
local strtable = {} | |
for i = 1, 1000 do | |
arraytable[i] = i | |
hashtable[1000 - i] = i | |
strtable[tostring(i)] = i | |
end | |
local function luafunction(a) | |
return a | |
end | |
local function uncachedengine() | |
local t0 = spGetTimerMicros() | |
local v = 0 | |
for i = 1, numloops do | |
v = v + Spring.GetGameFrame() | |
end | |
return Spring.DiffTimers(spGetTimerMicros(), t0) * (numloops / 1000000) | |
end | |
local function cachedengine() | |
local t0 = spGetTimerMicros() | |
local v = 0 | |
for i = 1, numloops do | |
v = v + spGetGameFrame() | |
end | |
return Spring.DiffTimers(spGetTimerMicros(), t0) * (numloops / 1000000) | |
end | |
local function noengine() | |
local t0 = spGetTimerMicros() | |
local v = 0 | |
for i = 1, numloops do | |
v = v + i | |
end | |
return Spring.DiffTimers(spGetTimerMicros(), t0) * (numloops / 1000000) | |
end | |
local function noengineluafunc() | |
local t0 = spGetTimerMicros() | |
local v = 0 | |
for i = 1, numloops do | |
v = v + luafunction(i) | |
end | |
return Spring.DiffTimers(spGetTimerMicros(), t0) * (numloops / 1000000) | |
end | |
local function noengineluafuncpcall() | |
local t0 = spGetTimerMicros() | |
local v = 0 | |
for i = 1, numloops do | |
local success, result = pcall(luafunction, i) | |
v = v + result | |
end | |
return Spring.DiffTimers(spGetTimerMicros(), t0) * (numloops / 1000000) | |
end | |
local function noengineluafuncpcallvararg(...) | |
local t0 = spGetTimerMicros() | |
local v = 0 | |
for i = 1, numloops do | |
local success, result = pcall(luafunction, i, ...) | |
v = v + result | |
end | |
return Spring.DiffTimers(spGetTimerMicros(), t0) * (numloops / 1000000) | |
end | |
local function arraytablelookup() | |
local t0 = spGetTimerMicros() | |
local v = 0 | |
for i = 1, numloops do | |
v = v + arraytable[1] | |
end | |
return Spring.DiffTimers(spGetTimerMicros(), t0) * (numloops / 1000000) | |
end | |
local function arraytablelookuplocal() | |
local t0 = spGetTimerMicros() | |
local v = 0 | |
local localarraytable = arraytable | |
for i = 1, numloops do | |
v = v + localarraytable[1] | |
end | |
return Spring.DiffTimers(spGetTimerMicros(), t0) * (numloops / 1000000) | |
end | |
local function hashtablelookup() | |
local t0 = spGetTimerMicros() | |
local v = 0 | |
for i = 1, numloops do | |
v = v + hashtable[1] | |
end | |
return Spring.DiffTimers(spGetTimerMicros(), t0) * (numloops / 1000000) | |
end | |
local function strconcat() | |
local t0 = spGetTimerMicros() | |
local v = "lawl" | |
for i = 1, numloops do | |
v = "lawl" .. "test" | |
end | |
return Spring.DiffTimers(spGetTimerMicros(), t0) * (numloops / 1000000) | |
end | |
local function strconcattostring() | |
local t0 = spGetTimerMicros() | |
local v = "lawl" | |
for i = 1, numloops do | |
v = "lawl" .. tostring(i) | |
end | |
return Spring.DiffTimers(spGetTimerMicros(), t0) * (numloops / 1000000) | |
end | |
local function strtablelookup() | |
local t0 = spGetTimerMicros() | |
local v = 0 | |
for i = 1, numloops do | |
v = v + strtable["1"] | |
end | |
return Spring.DiffTimers(spGetTimerMicros(), t0) | |
end | |
local ran = false | |
function widget:GameFrame(n) | |
if not ran and n %30 == 0 then | |
Spring.Echo('noengine: v = v + i ', noengine(), 'ns per iteration') | |
Spring.Echo('uncachedengine: v = v + Spring.GetGameFrame()', uncachedengine(), 'ns per iteration') | |
Spring.Echo('cachedengine: v = v + spGetGameFrame()', cachedengine(), 'ns per iteration') | |
Spring.Echo('noengineluafunc: v = v + luafunction(i)', noengineluafunc(), 'ns per iteration') | |
Spring.Echo('noengineluafuncpcall: v = v + pcall(luafunction(i))', noengineluafuncpcall(), 'ns per iteration') | |
Spring.Echo('noengineluafuncpcallvararg: v = v + pcall(luafunction(i),...)', noengineluafuncpcallvararg(), 'ns per iteration') | |
Spring.Echo('arraytablelookup: v = v + arraytable[1]', arraytablelookup(), 'ns per iteration') | |
Spring.Echo('arraytablelookuplocal: v = v + localarraytable[1]', arraytablelookuplocal(), 'ns per iteration') | |
Spring.Echo('hashtablelookup: v = v + hashtable[1]', hashtablelookup(), 'ns per iteration') | |
Spring.Echo('strconcattostring: v = "lawl" .. tostring(i)', strconcattostring(), 'ns per iteration') | |
Spring.Echo('strconcat: v = "lawl".. "test" ', strconcat(), 'ns per iteration') | |
Spring.Echo('strtablelookup: v = v + strtable["1"]', strtablelookup(), 'ns per iteration') | |
--ran = true | |
end | |
end | |
-- RESULTS: | |
--[[ | |
[t=00:27:48.883714][f=0047160] strtablelookup: v = v + strtable["1"], 13.6959991, ns per iteration | |
[t=00:27:49.387216][f=0047190] noengine: v = v + i , 6.9119997, ns per iteration | |
[t=00:27:49.417415][f=0047190] uncachedengine: v = v + Spring.GetGameFrame(), 30.0799999, ns per iteration | |
[t=00:27:49.438891][f=0047190] cachedengine: v = v + spGetGameFrame(), 21.3759995, ns per iteration | |
[t=00:27:49.457493][f=0047190] noengineluafunc: v = v + luafunction(i), 18.5599995, ns per iteration | |
[t=00:27:49.505042][f=0047190] noengineluafuncpcall: v = v + pcall(luafunction(i)), 47.6159973, ns per iteration | |
[t=00:27:49.556518][f=0047190] noengineluafuncpcallvararg: v = v + pcall(luafunction(i),...), 51.5839996, ns per iteration | |
[t=00:27:49.569696][f=0047190] arraytablelookup: v = v + arraytable[1], 13.0559998, ns per iteration | |
[t=00:27:49.581152][f=0047190] arraytablelookuplocal: v = v + localarraytable[1], 11.3919992, ns per iteration | |
[t=00:27:49.594322][f=0047190] hashtablelookup: v = v + hashtable[1], 13.0559998, ns per iteration | |
[t=00:27:50.069056][f=0047190] strconcattostring: v = "lawl" .. tostring(i), 474.751984, ns per iteration | |
[t=00:27:50.093519][f=0047190] strconcat: v = "lawl".. "test" , 24.5760002, ns per iteration | |
[t=00:27:50.107852][f=0047190] strtablelookup: v = v + strtable["1"], 14.3359995, ns per iteration | |
]]-- | |
-- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment