Advertisement
Kingdaro

speed test (map() vs pairs())

Nov 11th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.17 KB | None | 0 0
  1. -- define map function (pretending it's default in lua)
  2. local function map(t, f) for _, v in pairs(t) do f(v) end end
  3.  
  4. -- "randomTable" with 10,000 elements (numbers)
  5. local randomTable = (function()
  6.     local acc = {}
  7.     for i=1, 10000 do
  8.         table.insert(acc, i)
  9.     end
  10.     return acc
  11. end)()
  12.  
  13. local function mapAnonymous()
  14.     map(randomTable, function(element)
  15.         io.write(element .. ' ')
  16.     end)
  17.     print()
  18. end
  19.  
  20. local function mapDefined()
  21.     local function printElement(element)
  22.         io.write(element .. ' ')
  23.     end
  24.     map(randomTable, printElement)
  25.     print()
  26. end
  27.  
  28. local function pairsLoop()
  29.     for _, element in pairs(randomTable) do
  30.         io.write(element .. ' ')
  31.     end
  32.     print()
  33. end
  34.  
  35. local results = {
  36.     mapA = 0;
  37.     mapB = 0;
  38.     pairs = 0;
  39.     tests = 0;
  40. }
  41.  
  42. local function test()
  43.     -- for speed measurement
  44.     local oldTime, newTime
  45.    
  46.     -- mapping (with anonymous function)
  47.     oldTime = os.clock()
  48.     mapAnonymous()
  49.     newTime = os.clock()
  50.     results.mapA = results.mapA + (newTime - oldTime)
  51.    
  52.     -- mapping (defined function)
  53.     oldTime = os.clock()
  54.     mapDefined()
  55.     newTime = os.clock()
  56.     results.mapB = results.mapB + (newTime - oldTime)
  57.    
  58.     -- pairs
  59.     oldTime = os.clock()
  60.     pairsLoop()
  61.     newTime = os.clock()
  62.     results.pairs = results.pairs + (newTime - oldTime)
  63.  
  64.     results.tests = results.tests + 1
  65. end
  66.  
  67. local function averageResults()
  68.     for i,v in pairs(results) do
  69.         if i ~= 'tests' then
  70.             results[i] = v / results.tests
  71.         end
  72.     end
  73. end
  74.  
  75. local function printResults()
  76.     print('Mapping (anonymous function):',  results.mapA)
  77.     print('Mapping (defined function):',    results.mapB)
  78.     print('Using Pairs:',                   results.pairs)
  79. end
  80.  
  81. test()
  82. test()
  83. test()
  84. test()
  85. test()
  86. test()
  87. test()
  88. test()
  89. test()
  90. test()
  91.  
  92. averageResults()
  93. printResults()
  94.  
  95. --[[ results of 5 tests
  96.  
  97. Mapping (anonymous function):   0.0341
  98. Mapping (defined function): 0.0302
  99. Using Pairs:            0.0287 <-- winner
  100.  
  101. Mapping (anonymous function):   0.0329
  102. Mapping (defined function): 0.0276 <-- winner
  103. Using Pairs:            0.0315
  104.  
  105. Mapping (anonymous function):   0.0384
  106. Mapping (defined function): 0.0344
  107. Using Pairs:            0.031  <-- winner
  108.  
  109. Mapping (anonymous function):   0.0322
  110. Mapping (defined function): 0.0416
  111. Using Pairs:            0.0253 <-- winner
  112.  
  113. Mapping (anonymous function):   0.0448
  114. Mapping (defined function): 0.0262
  115. Using Pairs:            0.0259 <-- winner
  116. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement