Advertisement
TheOddByte

[ComputerCraft] Benchmark

Jun 6th, 2015
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.49 KB | None | 0 0
  1. --[[
  2.     Locals vs Globals benchmark test
  3.     @version 1.0, 2015-06-06
  4.     @author TheOddByte, Erik Hougaard
  5. --]]
  6.  
  7.  
  8.  
  9. function yield()
  10.     os.queueEvent( "sleep" )
  11.     coroutine.yield( "sleep" )
  12. end
  13.  
  14.  
  15. function sieve( n )
  16.     x = {}
  17.     iter = 0
  18.     repeat
  19.         x[1] = 0
  20.         i = 2
  21.         repeat
  22.             x[i] = 1
  23.             i = i + 1
  24.         until i > n
  25.         p = 2
  26.         while(p * p <= n) do
  27.             j = p
  28.             while(j <= n) do
  29.                 x[j] = 0
  30.                 j = j + p
  31.             end
  32.             repeat
  33.                 p = p + 1
  34.             until x[p] == 1
  35.         end
  36.         iter = iter + 1
  37.         yield() --# Yield, just to make sure it isn't erroring
  38.     until iter == 101
  39. end
  40.  
  41. function sieve_locals( n )
  42.     local x = {}
  43.     local iter = 0
  44.     repeat
  45.         x[1] = 0
  46.         local i = 2
  47.         repeat
  48.             x[i] = 1
  49.             i = i + 1
  50.         until i > n
  51.         local p = 2
  52.         while(p * p <= n) do
  53.             local j = p
  54.             while(j <= n) do
  55.                 x[j] = 0
  56.                 j = j + p
  57.             end
  58.             repeat
  59.                 p = p + 1
  60.             until x[p] == 1
  61.         end
  62.         iter = iter + 1
  63.         yield() --# Yield, just to make sure it isn't erroring
  64.     until iter == 101
  65. end
  66.  
  67.  
  68.  
  69. --# Store results in these tables
  70. local locals  = {}
  71. local globals = {}
  72.  
  73.  
  74. --# Indicate that the test has started
  75. local w, h = term.getSize()
  76. term.clear()
  77. term.setCursorPos( 1, 1 )
  78. print("Sieve of Eratosthenes - Lua Benchmark test 1.0\n" .. string.rep( "-", w ))
  79. print( "Original author: Erik Hougaard" )
  80. print( "Modified by: TheOddByte" )
  81. print("\n[".. textutils.formatTime( os.time(), true ) .. "] Started test")
  82.  
  83.  
  84. --# Test the function with local variables
  85. for i = 1, 10 do
  86.     local start = os.clock()
  87.     sieve_locals( 10000 )
  88.     local stop = os.clock()
  89.     table.insert( locals, ( stop - start ) )
  90. end
  91.  
  92. --# Test the function with global variables
  93. for i = 1, 10 do
  94.     local start = os.clock()
  95.     sieve( 10000 )
  96.     local stop = os.clock()
  97.     table.insert( globals, ( stop - start ) )
  98. end
  99. print("[".. textutils.formatTime( os.time(), true ) .. "] Finished test")
  100.  
  101.  
  102. --# Get the average time of both tests
  103. --# And print them to the screen
  104. local a, b = 0, 0
  105. for i = 1, #locals do
  106.     a = a + locals[i]
  107.     b = b + globals[i]
  108. end
  109. print("\nAverage time with locals: " .. ( a/#locals ) .. " s" )
  110. print("Average time with globals: " .. ( b/#globals ) .. " s" )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement