fork download
  1. t_start, t_end, result_num = nil
  2.  
  3.  
  4.  
  5. -- fill a table with 15 million random numbers
  6.  
  7. my_table = {}
  8.  
  9. for fillop = 1, 15000000 do
  10.  
  11. table.insert( my_table, math.random() )
  12.  
  13. end
  14.  
  15.  
  16.  
  17. -- test normal forloop to add all the numbers
  18.  
  19. result_num = 0
  20.  
  21. t_start = os.clock()
  22.  
  23. for i = 1, #my_table do
  24.  
  25. result_num = result_num + my_table[i]
  26.  
  27. end
  28.  
  29. t_end = os.clock()
  30.  
  31. print( "Normal for loop took " .. ( t_end - t_start ) .. " seconds to complete." )
  32.  
  33.  
  34.  
  35. -- test pairs() to add all the numbers
  36.  
  37. result_num = 0
  38.  
  39. t_start = os.clock()
  40.  
  41. for k,v in pairs( my_table ) do
  42.  
  43. result_num = result_num + v
  44.  
  45. end
  46.  
  47. t_end = os.clock()
  48.  
  49. print( "pairs took " .. ( t_end - t_start ) .. " seconds to complete." )
  50.  
  51.  
  52.  
  53. -- test next to add all the numbers
  54.  
  55. result_num = 0
  56.  
  57. t_start = os.clock()
  58.  
  59. for k,v in next, my_table, nil do
  60.  
  61. result_num = result_num + v
  62.  
  63. end
  64.  
  65. t_end = os.clock()
  66.  
  67. print( "next took " .. ( t_end - t_start ) .. " seconds to complete." )
Success #stdin #stdout 4.49s 14120KB
stdin
Standard input is empty
stdout
Normal for loop took 0.51942 seconds to complete.
pairs took 0.771917 seconds to complete.
next took 0.767419 seconds to complete.