Zamiel

Benchmark Mod

Nov 18th, 2017
645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.28 KB | None | 0 0
  1. -- +---------------+
  2. -- | Benchmark Mod |
  3. -- |   by Zamiel   |
  4. -- +---------------+
  5.  
  6. -- This is a mod that will benchmark two different functions to calculate
  7. -- how much more efficient one function is over the other.
  8.  
  9. local mod = RegisterMod("test-benchmark", 1);
  10.  
  11. -- Benchmark configuration
  12. mod.numTrials = 100000 -- This is how many times each of the two functions will be executed
  13.  
  14. -- Global variables
  15. mod.sfx = SFXManager()
  16. local averages = {}
  17.  
  18. -- ModCallbacks.MC_POST_GAME_STARTED (15)
  19. function mod:PostGameStarted()
  20.   Isaac.DebugString("MC_POST_GAME_STARTED")
  21.  
  22.   -- Get the two average times
  23.   for i = 1, 2 do
  24.     local totalTime = 0
  25.     for j = 1, mod.numTrials do
  26.       local startTime = Isaac.GetTime()
  27.       if i == 1 then
  28.         mod:Function1()
  29.       elseif i == 2 then
  30.         mod:Function2()
  31.       end
  32.       local endTime = Isaac.GetTime()
  33.       local elapsedTime = endTime - startTime
  34.       totalTime = totalTime + elapsedTime
  35.     end
  36.     local averageTime = totalTime / mod.numTrials
  37.     Isaac.DebugString("The average time of function " .. tostring(i) .. " is: " .. tostring(averageTime))
  38.     averages[i] = averageTime -- Store it for later
  39.   end
  40.  
  41.   -- Compare the two average times
  42.   local functionDifference = averages[1] - averages[2]
  43.   Isaac.DebugString("The difference is: " .. tostring(functionDifference))
  44.   local percent = functionDifference / averages[1] * 100
  45.   percent = round(percent, 2)
  46.   Isaac.DebugString("This is a difference of " .. tostring(percent) .. "%.")
  47. end
  48.  
  49. -- The slower function
  50. function mod:Function1()
  51.   local sfx = SFXManager()
  52.   sfx:Play(SoundEffect.SOUND_THUMBS_DOWN, 1, 0, false, 1) -- 267
  53. end
  54.  
  55. -- The faster function
  56. function mod:Function2()
  57.   mod.sfx:Play(SoundEffect.SOUND_THUMBS_DOWN, 1, 0, false, 1) -- 267
  58. end
  59.  
  60. -- Callback definitions
  61. mod:AddCallback(ModCallbacks.MC_POST_GAME_STARTED, mod.PostGameStarted)
  62.  
  63. -- Welcome message
  64. Isaac.DebugString("+-------------------------------+")
  65. Isaac.DebugString("| Bencharmking mod initialized. |")
  66. Isaac.DebugString("+-------------------------------+")
  67.  
  68. -- Miscellaneous helper functions
  69. -- From: http://lua-users.org/wiki/SimpleRound
  70. function round(num, numDecimalPlaces)
  71.   local mult = 10^(numDecimalPlaces or 0)
  72.   return math.floor(num * mult + 0.5) / mult
  73. end
Advertisement
Add Comment
Please, Sign In to add comment