Zamiel

Lua Benchmark Mod

Mar 5th, 2022 (edited)
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 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 how much more efficient
  7. -- one function is over the other.
  8.  
  9. local socket = require("socket")
  10.  
  11. local mod = RegisterMod("test-benchmark", 1)
  12.  
  13. -- Benchmark configuration
  14. local NUM_TRIALS_PER_FUNCTION = 1000
  15. local THRESHOLD_IN_SECONDS = 0.001 -- 1 millisecond
  16.  
  17. -- Variables
  18. local averages = {}
  19. local game = Game()
  20.  
  21. local function round(a, b)
  22. return (a - a % b) / b
  23. end
  24.  
  25. function __TS__ArrayFilter(arr, callbackfn)
  26. local result = {}
  27. do
  28. local i = 0
  29. while i < #arr do
  30. if callbackfn(_G, arr[i + 1], i, arr) then
  31. result[#result + 1] = arr[i + 1]
  32. end
  33. i = i + 1
  34. end
  35. end
  36. return result
  37. end
  38.  
  39. -- ModCallbacks.MC_POST_GAME_STARTED (15)
  40. function mod:PostGameStarted()
  41. Isaac.DebugString("Using a threshold in seconds of: " .. tostring(THRESHOLD_IN_SECONDS))
  42. local numInvocations = mod:GetNumInvocationsUntil()
  43. Isaac.DebugString("You would need to execute the slower function " .. tostring(numInvocations) .. " times per frame for it to ever make a difference.")
  44. end
  45.  
  46. function mod:GetNumInvocationsUntil()
  47. local totalTimeTaken = 0
  48. local i = 0
  49. while true do
  50. i = i + 1
  51.  
  52. local elapsedTimeSeconds1 = mod:GetExecutionTimeInSeconds(mod.Function1)
  53. Isaac.DebugString("Elapsed time for function 1: " .. tostring(elapsedTimeSeconds1))
  54. local elapsedTimeSeconds2 = mod:GetExecutionTimeInSeconds(mod.Function2)
  55. Isaac.DebugString("Elapsed time for function 2: " .. tostring(elapsedTimeSeconds2))
  56.  
  57. local difference = elapsedTimeSeconds2 - elapsedTimeSeconds1
  58. Isaac.DebugString("Difference: " .. tostring(difference))
  59. totalTimeTaken = totalTimeTaken + difference
  60. if totalTimeTaken >= NUM_TRIALS_PER_FUNCTION * THRESHOLD_IN_SECONDS then
  61. return i
  62. end
  63. end
  64. end
  65.  
  66. function mod:GetExecutionTimeInSeconds(func)
  67. local startTimeSeconds = socket:gettime()
  68. for i = 1, NUM_TRIALS_PER_FUNCTION do
  69. func()
  70. end
  71. local endTimeSeconds = socket:gettime()
  72.  
  73. return endTimeSeconds - startTimeSeconds
  74. end
  75.  
  76. function mod:Function1()
  77. -- Iteration
  78. local itemConfig = Isaac.GetItemConfig()
  79. local getMaxCollectibleType = itemConfig:GetCollectibles().Size - 1
  80.  
  81. local familiarCollectibleTypes = {}
  82. for collectibleType = 1, getMaxCollectibleType do
  83. local itemConfigItem = itemConfig:GetCollectible(collectibleType)
  84. if itemConfigItem ~= nil and itemConfigItem.Type == ItemType.ITEM_FAMILIAR then
  85. table.insert(familiarCollectibleTypes, collectibleType)
  86. end
  87. end
  88.  
  89. return familiarCollectibleTypes
  90. end
  91.  
  92. function mod:Function2()
  93. -- Functional
  94. local itemConfig = Isaac.GetItemConfig()
  95. local getMaxCollectibleType = itemConfig:GetCollectibles().Size - 1
  96.  
  97. local collectibleTypes = {}
  98. for collectibleType = 1, getMaxCollectibleType do
  99. table.insert(collectibleTypes, collectibleType)
  100. end
  101.  
  102. local function filterFamiliars(_, i)
  103. local itemConfigItem = itemConfig:GetCollectible(i)
  104. return itemConfigItem ~= nil and itemConfigItem.Type == ItemType.ITEM_FAMILIAR
  105. end
  106. return __TS__ArrayFilter(collectibleTypes, filterFamiliars)
  107. end
  108.  
  109. mod:AddCallback(ModCallbacks.MC_POST_GAME_STARTED, mod.PostGameStarted)
Advertisement
Add Comment
Please, Sign In to add comment