Advertisement
revolucas

[Lua] Benchmark of table references

Feb 28th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.46 KB | None | 0 0
  1. --[[
  2. output:
  3. ------------------------
  4. Control: 0.0090000000000146
  5. Test A: 0.02349999999999
  6. Test B: 0.1049
  7. Test C: 0.1067
  8. ------------------------
  9. conclusion: localize deeply nested variables before loop
  10. --]]
  11.  
  12. local clock = os.clock
  13.  
  14. local this = { is = { a = { deep = { tbl = { dude = true } } } } }
  15.  
  16. -- Control group: empty for loop for comparison to Test A and B
  17. local sum = 0
  18. local itr = 0
  19. repeat
  20.     local before = clock()
  21.     for i=1,1000000 do
  22.     end
  23.     sum = sum + (clock()-before)
  24.     itr = itr + 1
  25. until (itr == 10)
  26. local test_control = sum/itr -- The average time
  27.  
  28.  
  29. -- TEST A
  30. sum = 0
  31. itr = 0
  32. repeat
  33.     local is_a_dude = this.is.a.deep.tbl.dude
  34.     local before = clock()
  35.     for i=1,1000000 do
  36.         if (is_a_dude == true) then
  37.        
  38.         end
  39.     end
  40.     sum = sum + (clock()-before)
  41.     itr = itr + 1
  42. until (itr == 10)
  43. local test_a = sum/itr
  44.  
  45.  
  46. -- TEST B
  47. sum = 0
  48. itr = 0
  49. repeat
  50.     local before = clock()
  51.     for i=1,1000000 do
  52.         if (this.is.a.deep.tbl.dude == true) then
  53.        
  54.         end
  55.     end
  56.     sum = sum + (clock()-before)
  57.     itr = itr + 1
  58. until (itr == 10)
  59. local test_b = sum/itr
  60.  
  61. -- TEST C
  62. sum = 0
  63. itr = 0
  64. repeat
  65.     local before = clock()
  66.     for i=1,1000000 do
  67.         local is_a_dude = this.is.a.deep.tbl.dude
  68.         if (is_a_dude == true) then
  69.        
  70.         end
  71.     end
  72.     sum = sum + (clock()-before)
  73.     itr = itr + 1
  74. until (itr == 10)
  75. local test_c = sum/itr
  76.  
  77. print ("Control: "..test_control)
  78. print ("Test A: "..test_a)
  79. print ("Test B: "..test_b)
  80. print ("Test C: "..test_c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement