Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local toBinary = {}
- -- iPoisonxL
- toBinary['iPoisonxL'] = function (int)
- if(type(int)=="number")then
- local current = int
- local bin = {}
- while(current~=0)do
- table.insert(bin, current%2)
- current = math.floor(current / 2)
- end
- local result = ""
- for i,v in pairs(bin) do
- result = result .. v
- end
- return string.reverse(result)
- end
- return "error: string input"
- end
- -- Ref
- toBinary['Ref'] = function( int )
- assert( type( int ) == 'number', 'Error: Non-number input to toBinary!' )
- local current = math.floor( int )
- local result = ''
- while( current ~= 0 ) do
- result = result..current % 2
- current = math.floor( current / 2)
- end
- return ( '%08s' ):format( string.reverse( result ) )
- end
- -- time thief
- toBinary['time thief'] = function (number)
- local base = 2
- local result = {}
- while number > 0 do
- local digit = number % base
- result[#result + 1] = digit < 10 and digit or string.char(55 + digit)
- number = math.floor(number / base)
- end
- return string.reverse(table.concat(result))
- end
- -- Positive07
- local hex = {
- ["0"] = "0000", ["1"] = "0001", ["2"] = "0010", ["3"] = "0011",
- ["4"] = "0100", ["5"] = "0101", ["6"] = "0110", ["7"] = "0111",
- ["8"] = "1000", ["9"] = "1001", ["A"] = "1010", ["B"] = "1011",
- ["C"] = "1100", ["D"] = "1101", ["E"] = "1110", ["F"] = "1111",
- }
- local func = function (n) return hex[n] end
- toBinary['Positive07'] = function (n)
- local result = string.format("%X", n):gsub("(.)", func):gsub("^(0*)(.)","%2")
- return result
- end
- -- bartbes
- do
- local conv = {}
- for i = 0, 7 do
- conv[tostring(i)] = ("%d%d%d"):format(math.floor(i%8/4), math.floor(i%4/2), math.floor(i%2/1))
- end
- toBinary['bartbes'] = function(n)
- return (("%o"):format(n):gsub(".", conv):match("1.+$") or 0)
- end
- end
- -- Positive07 and bartbes
- do
- local conv = {}
- for i = 0, 7 do
- conv[tostring(i)] = ("%i%i%i"):format(i%8/4, i%4/2, i%2/1)
- end
- toBinary['Positive07 and bartbes'] = function(n)
- return (("%o"):format(n):gsub(".", conv):match("1.+$")) or "0"
- end
- end
- -- which one's fastest?
- local getTime = love.timer.getTime
- local function perf (name)
- collectgarbage()
- print('\n' .. name)
- local f = toBinary[name]
- local n = 0
- local ops = 0
- local endTime = getTime() + 10
- while getTime() < endTime do
- n = n + 1
- if n > 1000 then n = 1 end
- f(n)
- ops = ops + 1
- end
- print(('%d ops per second'):format(ops / 10))
- end
- function love.load ()
- perf('iPoisonxL')
- perf('Ref')
- perf('time thief')
- perf('Positive07')
- perf('bartbes')
- perf('Positive07 and bartbes')
- love.event.quit()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement