Advertisement
Belzebub

equential_table_loop_bench.lua

Mar 5th, 2021
750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.31 KB | None | 0 0
  1. -- gmod luajit
  2.  
  3. local function Benchmark(uid, func, count, onfinish)
  4.     timer.Simple(0, function() -- 1 tick delay
  5.         local start = SysTime()
  6.    
  7.         for i = 1, count do
  8.             func()    
  9.         end
  10.        
  11.         print(uid, SysTime() - start)
  12.        
  13.         if onfinish then onfinish() end
  14.     end)
  15. end
  16.  
  17. local function Bench()
  18.     local bench = {
  19.         process = {},
  20.         Start = function(self, count)
  21.             self.count = count
  22.             self:Run()
  23.         end,
  24.         Run = function(self, id, onfinish)
  25.             id = (id or 0) + 1
  26.             local process = self.process[id]
  27.             if process == nil then return end
  28.            
  29.             Benchmark(process.uid, process.func, self.count, function()
  30.                 self:Run(id)
  31.             end)
  32.         end,
  33.         Add = function(self, uid, func)
  34.             table.insert(self.process, {uid = uid, func = func})
  35.             return self
  36.         end
  37.     }
  38.     bench.__index = bench
  39.    
  40.     return bench
  41. end
  42.  
  43. local tbl = {"one", "two", "three", "four", "five"}
  44.  
  45. Bench()
  46. :Add("for i = 1, #tbl do", function()
  47.     for i = 1, #tbl do
  48.         local v = tbl[i]
  49.     end
  50. end)
  51. :Add("for i, v in ipairs(tbl) do", function()
  52.     for i, v in ipairs(tbl) do
  53.  
  54.     end
  55. end)
  56. :Start(10000000) -- 10 000 000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement