Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- +---------------+
- -- | Benchmark Mod |
- -- | 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 mod = RegisterMod("test-benchmark", 1);
- -- Benchmark configuration
- mod.numTrials = 100000 -- This is how many times each of the two functions will be executed
- -- Global variables
- mod.sfx = SFXManager()
- local averages = {}
- -- ModCallbacks.MC_POST_GAME_STARTED (15)
- function mod:PostGameStarted()
- Isaac.DebugString("MC_POST_GAME_STARTED")
- -- Get the two average times
- for i = 1, 2 do
- local totalTime = 0
- for j = 1, mod.numTrials do
- local startTime = Isaac.GetTime()
- if i == 1 then
- mod:Function1()
- elseif i == 2 then
- mod:Function2()
- end
- local endTime = Isaac.GetTime()
- local elapsedTime = endTime - startTime
- totalTime = totalTime + elapsedTime
- end
- local averageTime = totalTime / mod.numTrials
- Isaac.DebugString("The average time of function " .. tostring(i) .. " is: " .. tostring(averageTime))
- averages[i] = averageTime -- Store it for later
- end
- -- Compare the two average times
- local functionDifference = averages[1] - averages[2]
- Isaac.DebugString("The difference is: " .. tostring(functionDifference))
- local percent = functionDifference / averages[1] * 100
- percent = round(percent, 2)
- Isaac.DebugString("This is a difference of " .. tostring(percent) .. "%.")
- end
- -- The slower function
- function mod:Function1()
- local sfx = SFXManager()
- sfx:Play(SoundEffect.SOUND_THUMBS_DOWN, 1, 0, false, 1) -- 267
- end
- -- The faster function
- function mod:Function2()
- mod.sfx:Play(SoundEffect.SOUND_THUMBS_DOWN, 1, 0, false, 1) -- 267
- end
- -- Callback definitions
- mod:AddCallback(ModCallbacks.MC_POST_GAME_STARTED, mod.PostGameStarted)
- -- Welcome message
- Isaac.DebugString("+-------------------------------+")
- Isaac.DebugString("| Bencharmking mod initialized. |")
- Isaac.DebugString("+-------------------------------+")
- -- Miscellaneous helper functions
- -- From: http://lua-users.org/wiki/SimpleRound
- function round(num, numDecimalPlaces)
- local mult = 10^(numDecimalPlaces or 0)
- return math.floor(num * mult + 0.5) / mult
- end
Advertisement
Add Comment
Please, Sign In to add comment