Skip to content

Instantly share code, notes, and snippets.

@DarkWiiPlayer
Last active May 6, 2022 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DarkWiiPlayer/a0360f8ae3b54ad79edc08e38fffd699 to your computer and use it in GitHub Desktop.
Save DarkWiiPlayer/a0360f8ae3b54ad79edc08e38fffd699 to your computer and use it in GitHub Desktop.
local based = require 'based.32.crockford'
local basexx = require 'basexx'
local function bench(name, fn, vector)
local before = os.clock()
for _, input in ipairs(vector) do
fn(input)
end
local after = os.clock()
print(string.format("%-10s %1.6f seconds", name..":", after-before))
end
local function benchAll(subjects, vector)
for name, fn in pairs(subjects) do
bench(name, fn, vector)
end
end
local function compare(subjects, vector)
benchAll(subjects, vector)
for i = 1, 10 do
local item = vector[i]
local ref = next(subjects)
local result = subjects[ref](item)
for name in next, subjects, ref do
local new = subjects[name](item)
if new ~= result then
print("Mismatch: "..result.." vs. "..new)
end
end
end
end
-----
print(_VERSION)
---
local strings = {}
for i = 1, 1e3 do
if not strings[i] then
strings[i] = string.rep("x", 20):gsub(".", function() return string.char(math.random(("a"):byte(), ("z"):byte())) end)
end
end
print("Encoding:")
compare({
Based = based.encode,
BaseXX = basexx.to_crockford,
}, strings)
print "-----"
local encoded = {}
for i = 1, 1e3 do
if not encoded[i] then
encoded[i] = based.encode(strings[i])
end
end
print("Decoding: ")
compare({
Based = based.decode,
BaseXX = basexx.from_crockford,
}, encoded)

Why?

I recently started my own Lua project combining encoders and decoders for different Base-N encodings and only after pushing it to luarocks.org found out that such a project already exists, so I decided to benchmark them both to see if my new code is at least faster. Lo and behold, yes.


Results

Encoding

Lua Based BaseXX ratio
Lua 0.010629 0.078772 x0.13
JIT (on) 0.002530 0.024218 x0.1
JIT (off) 0.005205 0.034397 x0.15

Decoding

Lua Based BaseXX ratio
Lua 0.009658 0.080785 x0.12
JIT (on) 0.002203 0.037336 x0.06
JIT (off) 0.004537 0.037538 x0.12

Note: Tests for incorrect decoding have been removed, but the corresponding code has been kept, as it has no real effect on the output.

$ lua -v
Lua 5.4.3 Copyright (C) 1994-2021 Lua.org, PUC-Rio
$ lua comparison.lua
Lua 5.4
Encoding:
Based: 0.010629 seconds
BaseXX: 0.078772 seconds
-----
Decoding:
Based: 0.009658 seconds
BaseXX: 0.080785 seconds
$ luajit -v
LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/
$ luajit comparison.lua
Lua 5.1
Encoding:
BaseXX: 0.024218 seconds
Based: 0.002530 seconds
-----
Decoding:
BaseXX: 0.037336 seconds
Based: 0.002203 seconds
$ luajit -joff comparison.lua
Lua 5.1
Encoding:
BaseXX: 0.034397 seconds
Based: 0.005205 seconds
-----
Decoding:
BaseXX: 0.037538 seconds
Based: 0.004537 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment