Advertisement
revolucas

[Lua] Benchmark of localization inside for loop

Feb 28th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. --[[
  2. output:
  3. ----------------------------
  4. Control: 0.0091000000000008
  5. Test A: 0.03180000000001
  6. Test B: 0.031799999999998
  7. ----------------------------
  8. conclusion: Difference is negligible if you localize outside loop
  9.  
  10. Note, if you remove local a,b,c from Test A here are the results:
  11. -----------------------------------
  12. Control: 0.0089999999999918
  13. Test A: 0.1145
  14. Test B: 0.033200000000011
  15. -----------------------------------
  16. conclusion: localize globals
  17. --]]
  18.  
  19. local clock = os.clock
  20.  
  21. -- Control group: empty for loop for comparison to Test A and B
  22. local sum = 0
  23. local itr = 0
  24. repeat
  25. local before = clock()
  26. for i=1,1000000 do
  27. end
  28. sum = sum + (clock()-before)
  29. itr = itr + 1
  30. until (itr == 10)
  31. local test_control = sum/itr -- The average time
  32.  
  33.  
  34. -- TEST A
  35. sum = 0
  36. itr = 0
  37. repeat
  38. local a,b,c
  39. local before = clock()
  40. for i=1,1000000 do
  41. a = 2
  42. b = 2
  43. c = a + b
  44. end
  45. sum = sum + (clock()-before)
  46. itr = itr + 1
  47. until (itr == 10)
  48. local test_a = sum/itr
  49.  
  50.  
  51. -- TEST B
  52. sum = 0
  53. itr = 0
  54. repeat
  55. local before = clock()
  56. for i=1,1000000 do
  57. local a = 2
  58. local b = 2
  59. local c = a + b
  60. end
  61. sum = sum + (clock()-before)
  62. itr = itr + 1
  63. until (itr == 10)
  64. local test_b = sum/itr
  65.  
  66. print ("Control: "..test_control)
  67. print ("Test A: "..test_a)
  68. print ("Test B: "..test_b)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement