Advertisement
Guest User

Benchmark

a guest
Feb 2nd, 2014
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.96 KB | None | 0 0
  1. print("Graphical and Speed Benchmark Test")
  2. print("This may take up to a minute")
  3. print("Press any key to start...")
  4. os.pullEvent("key")
  5. local wid, hei = term.getSize()
  6.  
  7. local function plainText()
  8.     term.clear()
  9.     for i = 1, hei do
  10.         term.setCursorPos(1,i)
  11.         for b = 1, wid do
  12.             term.write("X")
  13.         end
  14.     end
  15. end
  16.  
  17. local function rainbow()
  18.     term.clear()
  19.     for i = 0, 15 do
  20.         term.setBackgroundColor(2^i)
  21.         term.clear()
  22.     end
  23.     term.setBackgroundColor(colors.black)
  24. end
  25.  
  26. local function mix()
  27.     term.clear()
  28.     for b = 0, 15 do
  29.         for i = 1, hei, 2 do
  30.             term.setCursorPos(1,i)
  31.             term.setBackgroundColor(2^b)
  32.             term.clearLine()
  33.             term.setTextColor(2^b)
  34.             term.setCursorPos(1,i+1)
  35.             term.setBackgroundColor(colors.black)
  36.             for i = 1, wid do
  37.                 term.write(tostring(math.random(0,9)))
  38.             end
  39.         end
  40.     end
  41.     term.setBackgroundColor(colors.black)
  42.     term.clear()
  43. end
  44.  
  45. -- HASHES OMFG! --
  46.  
  47. local MOD = 2^32
  48. local MODM = MOD-1
  49.  
  50. local function memoize(f)
  51.         local mt = {}
  52.         local t = setmetatable({}, mt)
  53.         function mt:__index(k)
  54.                 local v = f(k)
  55.                 t[k] = v
  56.                 return v
  57.         end
  58.         return t
  59. end
  60.  
  61. local function make_bitop_uncached(t, m)
  62.         local function bitop(a, b)
  63.                 local res,p = 0,1
  64.                 while a ~= 0 and b ~= 0 do
  65.                         local am, bm = a % m, b % m
  66.                         res = res + t[am][bm] * p
  67.                         a = (a - am) / m
  68.                         b = (b - bm) / m
  69.                         p = p*m
  70.                 end
  71.                 res = res + (a + b) * p
  72.                 return res
  73.         end
  74.         return bitop
  75. end
  76.  
  77. local function make_bitop(t)
  78.         local op1 = make_bitop_uncached(t,2^1)
  79.         local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
  80.         return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  81. end
  82.  
  83. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  84.  
  85. local function bxor(a, b, c, ...)
  86.         local z = nil
  87.         if b then
  88.                 a = a % MOD
  89.                 b = b % MOD
  90.                 z = bxor1(a, b)
  91.                 if c then z = bxor(z, c, ...) end
  92.                 return z
  93.         elseif a then return a % MOD
  94.         else return 0 end
  95. end
  96.  
  97. local function band(a, b, c, ...)
  98.         local z
  99.         if b then
  100.                 a = a % MOD
  101.                 b = b % MOD
  102.                 z = ((a + b) - bxor1(a,b)) / 2
  103.                 if c then z = bit32_band(z, c, ...) end
  104.                 return z
  105.         elseif a then return a % MOD
  106.         else return MODM end
  107. end
  108.  
  109. local function bnot(x) return (-1 - x) % MOD end
  110.  
  111. local function rshift1(a, disp)
  112.         if disp < 0 then return lshift(a,-disp) end
  113.         return math.floor(a % 2 ^ 32 / 2 ^ disp)
  114. end
  115.  
  116. local function rshift(x, disp)
  117.         if disp > 31 or disp < -31 then return 0 end
  118.         return rshift1(x % MOD, disp)
  119. end
  120.  
  121. local function lshift(a, disp)
  122.         if disp < 0 then return rshift(a,-disp) end
  123.         return (a * 2 ^ disp) % 2 ^ 32
  124. end
  125.  
  126. local function rrotate(x, disp)
  127.     x = x % MOD
  128.     disp = disp % 32
  129.     local low = band(x, 2 ^ disp - 1)
  130.     return rshift(x, disp) + lshift(low, 32 - disp)
  131. end
  132.  
  133. local k = {
  134.         0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  135.         0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  136.         0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  137.         0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  138.         0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  139.         0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  140.         0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  141.         0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  142.         0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  143.         0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  144.         0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  145.         0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  146.         0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  147.         0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  148.         0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  149.         0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  150. }
  151.  
  152. local function str2hexa(s)
  153.         return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  154. end
  155.  
  156. local function num2s(l, n)
  157.         local s = ""
  158.         for i = 1, n do
  159.                 local rem = l % 256
  160.                 s = string.char(rem) .. s
  161.                 l = (l - rem) / 256
  162.         end
  163.         return s
  164. end
  165.  
  166. local function s232num(s, i)
  167.         local n = 0
  168.         for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  169.         return n
  170. end
  171.  
  172. local function preproc(msg, len)
  173.         local extra = 64 - ((len + 9) % 64)
  174.         len = num2s(8 * len, 8)
  175.         msg = msg .. "\128" .. string.rep("\0", extra) .. len
  176.         assert(#msg % 64 == 0)
  177.         return msg
  178. end
  179.  
  180. local function initH256(H)
  181.         H[1] = 0x6a09e667
  182.         H[2] = 0xbb67ae85
  183.         H[3] = 0x3c6ef372
  184.         H[4] = 0xa54ff53a
  185.         H[5] = 0x510e527f
  186.         H[6] = 0x9b05688c
  187.         H[7] = 0x1f83d9ab
  188.         H[8] = 0x5be0cd19
  189.         return H
  190. end
  191.  
  192. local function digestblock(msg, i, H)
  193.         local w = {}
  194.         for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  195.         for j = 17, 64 do
  196.                 local v = w[j - 15]
  197.                 local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  198.                 v = w[j - 2]
  199.                 w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  200.         end
  201.  
  202.         local a, b, c, d, e, f, g, h = H[1], H[2], H[3], H[4], H[5], H[6], H[7], H[8]
  203.         for i = 1, 64 do
  204.                 local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  205.                 local maj = bxor(band(a, b), band(a, c), band(b, c))
  206.                 local t2 = s0 + maj
  207.                 local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  208.                 local ch = bxor (band(e, f), band(bnot(e), g))
  209.                 local t1 = h + s1 + ch + k[i] + w[i]
  210.                 h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  211.         end
  212.  
  213.         H[1] = band(H[1] + a)
  214.         H[2] = band(H[2] + b)
  215.         H[3] = band(H[3] + c)
  216.         H[4] = band(H[4] + d)
  217.         H[5] = band(H[5] + e)
  218.         H[6] = band(H[6] + f)
  219.         H[7] = band(H[7] + g)
  220.         H[8] = band(H[8] + h)
  221. end
  222.  
  223. local function sha256(msg)
  224.         msg = preproc(msg, #msg)
  225.         local H = initH256({})
  226.         for i = 1, #msg, 64 do digestblock(msg, i, H) end
  227.         return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  228.                 num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  229. end
  230.  
  231. local function maths()
  232.     sha256([[CALCULATTIONNSSSS ARE OVERRRRR 900000000!!!11!!111111!1 OMFG GOOD LUCK WITH THIS SHIT!]])
  233. end
  234.  
  235. local repeat1 = 10
  236. local repeat2 = 50
  237. local start = os.clock()
  238. for i = 1, repeat1 do
  239.     for b = 1, repeat2 do
  240.         plainText()
  241.     end
  242.     sleep(0.1)
  243. end
  244. local plain = os.clock()-start
  245. start = os.clock()
  246. sleep(0.1)
  247. for i = 1, repeat1 do
  248.     for b = 1, repeat2*50 do
  249.         rainbow()
  250.     end
  251.     sleep(0.1)
  252. end
  253. local rainbows = os.clock()-start
  254. sleep(0.1)
  255. start = os.clock()
  256. for i = 1, repeat1 do
  257.     for b = 1, repeat2/5 do
  258.         mix()
  259.     end
  260.     sleep(0.1)
  261. end
  262. local mixed = os.clock()-start
  263. term.setCursorPos(1,1)
  264. term.setTextColor(colors.yellow)
  265. term.write("Final task...")
  266. sleep(0.1)
  267. start = os.clock()
  268. for i = 1, repeat1 do
  269.     for b = 1, repeat2/5 do
  270.         maths()
  271.     end
  272.     sleep(0.1)
  273. end
  274. local hash = os.clock()-start
  275.  
  276. term.clear()
  277. term.setCursorPos(1,1)
  278. term.setTextColor(colors.yellow)
  279. print("Text Rendering:     "..plain)
  280. print("Color Rendering:    "..rainbows)
  281. print("Advanced Rendering: "..mixed)
  282. print("Computing Power:    "..hash)
  283. print("The lower the number, the better")
  284. print("---------------------------")
  285. print("Total (Out of 50):  "..(55-(plain+rainbows+mixed+hash)))
  286. print("The higher the better for the total")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement