Advertisement
Guest User

tobinary perf test

a guest
Jul 18th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.86 KB | None | 0 0
  1. local toBinary = {}
  2.  
  3. -- iPoisonxL
  4. toBinary['iPoisonxL'] = function (int)
  5.    if(type(int)=="number")then
  6.       local current = int
  7.       local bin = {}
  8.       while(current~=0)do
  9.          table.insert(bin, current%2)
  10.          current = math.floor(current / 2)
  11.       end
  12.       local result = ""
  13.       for i,v in pairs(bin) do
  14.          result = result .. v
  15.       end
  16.       return string.reverse(result)
  17.    end
  18.    return "error: string input"
  19. end
  20.  
  21. -- Ref
  22. toBinary['Ref'] = function( int )
  23.    assert( type( int ) == 'number', 'Error: Non-number input to toBinary!' )
  24.    local current   = math.floor( int )
  25.    local result   = ''
  26.    while( current   ~= 0 ) do
  27.       result      = result..current % 2
  28.       current      = math.floor( current / 2)
  29.       end
  30.    return ( '%08s' ):format( string.reverse( result ) )
  31.    end
  32.  
  33.  
  34. -- time thief
  35. toBinary['time thief'] = function  (number)
  36.     local base = 2
  37.     local result = {}
  38.    
  39.     while number > 0 do
  40.         local digit = number % base
  41.         result[#result + 1] = digit < 10 and digit or string.char(55 + digit)
  42.         number = math.floor(number / base)
  43.     end
  44.    
  45.     return string.reverse(table.concat(result))
  46. end
  47.  
  48.  
  49. -- Positive07
  50. local hex = {
  51.     ["0"] = "0000", ["1"] = "0001", ["2"] = "0010", ["3"] = "0011",
  52.     ["4"] = "0100", ["5"] = "0101", ["6"] = "0110", ["7"] = "0111",
  53.     ["8"] = "1000", ["9"] = "1001", ["A"] = "1010", ["B"] = "1011",
  54.     ["C"] = "1100", ["D"] = "1101", ["E"] = "1110", ["F"] = "1111",
  55. }
  56.  
  57. local func = function (n) return hex[n] end
  58.  
  59. toBinary['Positive07'] = function (n)
  60.     local result = string.format("%X", n):gsub("(.)", func):gsub("^(0*)(.)","%2")
  61.     return result
  62. end
  63.  
  64. -- bartbes
  65. do
  66.    local conv = {}
  67.  
  68.    for i = 0, 7 do
  69.       conv[tostring(i)] = ("%d%d%d"):format(math.floor(i%8/4), math.floor(i%4/2), math.floor(i%2/1))
  70.    end
  71.  
  72.    toBinary['bartbes'] = function(n)
  73.       return (("%o"):format(n):gsub(".", conv):match("1.+$") or 0)
  74.    end
  75. end
  76.  
  77. -- Positive07 and bartbes
  78. do
  79.    local conv = {}
  80.  
  81.    for i = 0, 7 do
  82.       conv[tostring(i)] = ("%i%i%i"):format(i%8/4, i%4/2, i%2/1)
  83.    end
  84.  
  85.    toBinary['Positive07 and bartbes'] = function(n)
  86.       return (("%o"):format(n):gsub(".", conv):match("1.+$")) or "0"
  87.    end
  88. end
  89.  
  90. -- which one's fastest?
  91.  
  92. local getTime = love.timer.getTime
  93.  
  94. local function perf (name)
  95.     collectgarbage()
  96.     print('\n' .. name)
  97.     local f = toBinary[name]
  98.     local n = 0
  99.     local ops = 0
  100.     local endTime = getTime() + 10
  101.     while getTime() < endTime do
  102.         n = n + 1
  103.         if n > 1000 then n = 1 end
  104.         f(n)
  105.         ops = ops + 1
  106.     end
  107.     print(('%d ops per second'):format(ops / 10))
  108. end
  109.  
  110. function love.load ()
  111.     perf('iPoisonxL')
  112.     perf('Ref')
  113.     perf('time thief')
  114.     perf('Positive07')
  115.     perf('bartbes')
  116.     perf('Positive07 and bartbes')
  117.     love.event.quit()
  118. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement