Zamiel

Lua Benchmark Mod 2

Mar 5th, 2022 (edited)
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 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 game = Game()
  79. local room = game:GetRoom()
  80.  
  81. local nonSecretDoors = {}
  82. for i = 0, 7 do
  83. local door = room:GetDoor(i)
  84. if (
  85. door ~= nil
  86. and door.TargetRoomType ~= RoomType.ROOM_SECRET
  87. and door.TargetRoomType ~= RoomType.ROOM_SUPERSECRET
  88. ) then
  89. table.insert(nonSecretDoors, door)
  90. end
  91. end
  92.  
  93. return nonSecretDoors
  94. end
  95.  
  96. function mod:Function2()
  97. -- Functional
  98. local game = Game()
  99. local room = game:GetRoom()
  100.  
  101. local doors = {}
  102. for i = 0, 7 do
  103. local door = room:GetDoor(i)
  104. if door ~= nil then
  105. table.insert(doors, door)
  106. end
  107. end
  108.  
  109. local function filterDoors(_, door)
  110. return (
  111. door.TargetRoomType ~= RoomType.ROOM_SECRET
  112. and door.TargetRoomType ~= RoomType.ROOM_SUPERSECRET
  113. )
  114. end
  115. return __TS__ArrayFilter(doors, filterDoors)
  116. end
  117.  
  118. mod:AddCallback(ModCallbacks.MC_POST_GAME_STARTED, mod.PostGameStarted)
Advertisement
Add Comment
Please, Sign In to add comment