Advertisement
code_gs

Loop bench

Aug 20th, 2020
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. local SysTime = SysTime
  2. local print = print
  3.  
  4. local t = {}
  5. for i = 1, 1e3 do
  6. t[i] = i
  7. end
  8.  
  9. do
  10. local function b(t)
  11. local total = 0
  12.  
  13. for _, v in ipairs(t) do
  14. total = total + v
  15. end
  16. end
  17.  
  18. local nStart = SysTime()
  19.  
  20. for i = 1, 1e6 do
  21. b(t)
  22. end
  23.  
  24. print("ipairs", SysTime() - nStart)
  25. end
  26.  
  27. do
  28. local function b(t)
  29. local total = 0
  30.  
  31. for i = 1, 1e3 do
  32. total = total + t[i]
  33. end
  34. end
  35.  
  36. local nStart = SysTime()
  37.  
  38. for i = 1, 1e6 do
  39. b(t)
  40. end
  41.  
  42. print("for", SysTime() - nStart)
  43. end
  44.  
  45. do
  46. local function b(t)
  47. local total = 0
  48.  
  49. for i = 1e3, 1, -1 do
  50. total = total + t[i]
  51. end
  52. end
  53.  
  54. local nStart = SysTime()
  55.  
  56. for i = 1, 1e6 do
  57. b(t)
  58. end
  59.  
  60. print("reverse for", SysTime() - nStart)
  61. end
  62.  
  63. do
  64. local function b(t)
  65. local total = 0
  66. local i = 0
  67.  
  68. ::GOTO::
  69. i = i + 1
  70. total = total + t[i]
  71. if (i ~= 1e3) then
  72. goto GOTO
  73. end
  74. end
  75.  
  76. local nStart = SysTime()
  77.  
  78. for i = 1, 1e6 do
  79. b(t)
  80. end
  81.  
  82. print("goto", SysTime() - nStart)
  83. end
  84.  
  85. do
  86. local function b(t)
  87. local total = 0
  88. local i = 1e3
  89.  
  90. ::GOTO_REVERSE::
  91. total = total + t[i]
  92. i = i - 1
  93. if (i ~= 0) then
  94. goto GOTO_REVERSE
  95. end
  96. end
  97.  
  98. local nStart = SysTime()
  99.  
  100. for i = 1, 1e6 do
  101. b(t)
  102. end
  103.  
  104. print("reverse goto", SysTime() - nStart)
  105. end
  106.  
  107. do
  108. local function b(t)
  109. local total = 0
  110. local i = 0
  111.  
  112. while (i ~= 1e3) do
  113. i = i + 1
  114. total = total + t[i]
  115. end
  116. end
  117.  
  118. local nStart = SysTime()
  119.  
  120. for i = 1, 1e6 do
  121. b(t)
  122. end
  123.  
  124. print("while", SysTime() - nStart)
  125. end
  126.  
  127. do
  128. local function b(t)
  129. local total = 0
  130. local i = 1e3
  131.  
  132. while (i ~= 0) do
  133. total = total + t[i]
  134. i = i - 1
  135. end
  136. end
  137.  
  138. local nStart = SysTime()
  139.  
  140. for i = 1, 1e6 do
  141. b(t)
  142. end
  143.  
  144. print("reverse while", SysTime() - nStart)
  145. end
  146.  
  147. do
  148. local function b(t)
  149. local total = 0
  150. local i = 0
  151.  
  152. repeat
  153. i = i + 1
  154. total = total + t[i]
  155. until (i == 1e3)
  156. end
  157.  
  158. local nStart = SysTime()
  159.  
  160. for i = 1, 1e6 do
  161. b(t)
  162. end
  163.  
  164. print("repeat until", SysTime() - nStart)
  165. end
  166.  
  167. do
  168. local function b(t)
  169. local total = 0
  170. local i = 1e3
  171.  
  172. repeat
  173. total = total + t[i]
  174. i = i - 1
  175. until (i == 0)
  176. end
  177.  
  178. local nStart = SysTime()
  179.  
  180. for i = 1, 1e6 do
  181. b(t)
  182. end
  183.  
  184. print("reverse repeat until", SysTime() - nStart)
  185. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement