Advertisement
DapperCow15

Untitled

May 19th, 2025
3
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.86 KB | Source Code | 0 0
  1. --!native
  2. numbers = {}
  3. dictionary = {}
  4.  
  5. for i=1,1e7 do
  6.     numbers[#numbers+1] = true
  7.     dictionary[tostring(i)] = true
  8. end
  9.  
  10. local iterator = function(tbl, index)
  11.     local nextIndex = index + 1
  12.     local v = tbl[nextIndex]
  13.     if v ~= nil then
  14.         return nextIndex, v
  15.     end
  16. end
  17.  
  18. local numbers = setmetatable(numbers, {
  19.     __iter = function(tbl)
  20.         return iterator, tbl, 0
  21.     end,
  22. })
  23.  
  24. local dictionary = setmetatable(numbers, {
  25.     __iter = function(tbl)
  26.         return next, tbl, nil
  27.     end,
  28. })
  29.  
  30. task.wait()
  31.  
  32. --ipairs numbers
  33. local t = os.clock()
  34. local value = true
  35. for i,v in ipairs(numbers) do
  36.     value = value and v --you need to do something otherwise it won't be a meaningful test
  37. end
  38. local dt_ipairs_list = os.clock() - t
  39.  
  40. task.wait()
  41.  
  42. --pairs numbers
  43. local t = os.clock()
  44. local value = true
  45. for i,v in pairs(numbers) do
  46.     value = value and v --you need to do something otherwise it won't be a meaningful test
  47. end
  48. local dt_pairs_list = os.clock() - t
  49.  
  50. task.wait()
  51.  
  52. --pairs dictionary
  53. local t = os.clock()
  54. local value = true
  55. for i,v in pairs(dictionary) do
  56.     value = value and v --you need to do something otherwise it won't be a meaningful test
  57. end
  58. local dt_pairs_dict = os.clock() - t
  59.  
  60. task.wait()
  61.  
  62. --no pairs numbers
  63. local t = os.clock()
  64. local value = true
  65. for i,v in numbers do
  66.     value = value and v --you need to do something otherwise it won't be a meaningful test
  67. end
  68. local dt_nopairs_list = os.clock() - t
  69.  
  70. task.wait()
  71.  
  72. --no pairs dictionary
  73. local t = os.clock()
  74. local value = true
  75. for i,v in dictionary do
  76.     value = value and v --you need to do something otherwise it won't be a meaningful test
  77. end
  78. local dt_nopairs_dict = os.clock() - t
  79.  
  80. task.wait()
  81.  
  82. print("ipairs list: "..dt_ipairs_list)
  83. print("pairs list: "..dt_pairs_list)
  84. print("pairs dict: "..dt_pairs_dict)
  85. print("nopairs list: "..dt_nopairs_list)
  86. print("nopairs dict: "..dt_nopairs_dict)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement