-- +-----------------+ -- | Benchmark Mod 2 | -- | by Zamiel | -- +-----------------+ -- This is a mod that will benchmark two different functions to calculate how much more efficient -- one function is over the other. local socket = require("socket") local constants = require("constants") local mod = RegisterMod("test-benchmark", 1) -- Benchmark configuration local NUM_TRIALS_PER_FUNCTION = 100000 local THRESHOLD_IN_SECONDS = 0.001 -- 1 millisecond -- Variables local enabled = true local totalTimeTaken = 0 local i = 0 -- ModCallbacks.MC_POST_RENDER (2) function mod:postRender() if not enabled then return end i = i + 1 local elapsedTimeSeconds1 = mod:GetExecutionTimeInSeconds(mod.Function1) local elapsedTimeSeconds2 = mod:GetExecutionTimeInSeconds(mod.Function2) local difference = elapsedTimeSeconds2 - elapsedTimeSeconds1 totalTimeTaken = totalTimeTaken + difference if totalTimeTaken >= NUM_TRIALS_PER_FUNCTION * THRESHOLD_IN_SECONDS then Isaac.DebugString("i = " .. tostring(i)) enabled = false return end if i % 10 == 0 then Isaac.DebugString("On invocation: " .. tostring(i)) Isaac.DebugString("Total time taken: " .. tostring(totalTimeTaken)) Isaac.DebugString("Total time needs to be: " .. tostring(NUM_TRIALS_PER_FUNCTION * THRESHOLD_IN_SECONDS)) end end function mod:GetExecutionTimeInSeconds(func) local startTimeSeconds = socket:gettime() for i = 1, NUM_TRIALS_PER_FUNCTION do func() end local endTimeSeconds = socket:gettime() return endTimeSeconds - startTimeSeconds end -- Faster function function mod:Function1() local gameFrameCount = constants.GAME:GetFrameCount() return gameFrameCount end -- Slower function function mod:Function2() local game = Game() local gameFrameCount = game:GetFrameCount() return gameFrameCount end mod:AddCallback(ModCallbacks.MC_POST_RENDER, mod.postRender)