Skip to content

Instantly share code, notes, and snippets.

@muniter
Last active November 18, 2021 13:08
Show Gist options
  • Save muniter/9e6d0e86287307df9b980ddce5481a7b to your computer and use it in GitHub Desktop.
Save muniter/9e6d0e86287307df9b980ddce5481a7b to your computer and use it in GitHub Desktop.
Lua patterns and vim regex, not a real benchmark
-- Benchmarking support.
do
local function runner(name, code, ob, count)
local f = loadstring([[
local count,ob = ...
local clock = os.clock
local start = clock()
for i=1,count do ]] .. code .. [[ end
return clock() - start
]])
print(name, f(count, ob))
end
function Runner(list)
for _, bench in ipairs(list) do
for i, item in ipairs(bench.list) do
if i == 1 then
print("Benchmark " .. bench.title .. " n: " .. bench.count)
end
runner(item.name, item.code, item.ob, bench.count)
end
end
end
end
function Vim_stringfind(word, regex)
local regprog = vim.regex(regex)
return function()
regprog:match_str(word)
end
end
function Lua_stringfind(word, regex)
return function()
string.find(word, regex)
end
end
local luap = "Lua string.find:"
local vimr = "Vim regex :"
Runner({
{
title = [[Simple substring 'zi' in 'this is amazing']],
count = 10000000,
list = {
{ name = vimr, code = "ob()", ob = Vim_stringfind('this is amazing', [[zi]]) },
{ name = luap, code = "ob()", ob = Lua_stringfind('this is amazing', [[zi]]) }
}
},
{
title = [[Simple pattern l:'%d%d%d%l' v:'\d\d\d\l' 'this is ama(231c)zing']],
count = 1000000,
list = {
{ name = vimr, code = "ob()", ob = Vim_stringfind('this is ama(231c)zing', [[\d\d\d\l]]) },
{ name = luap, code = "ob()", ob = Lua_stringfind('this is ama(231c)zing', [[%d%d%d%l]]) }
}
},
{
title = [[Greedy star '2.*' 'this is ama(231c)zing']],
count = 1000000,
list = {
{ name = vimr, code = "ob()", ob = Vim_stringfind('this is ama(231c)zing', [[2.*]]) },
{ name = luap, code = "ob()", ob = Lua_stringfind('this is ama(231c)zing', [[2.*]]) }
}
},
{
title = [[Greedy with sets and anchored '[^c]*$' 'this is ama(231c)zing']],
count = 1000000,
list = {
{ name = vimr, code = "ob()", ob = Vim_stringfind('this is ama(231c)zing', "[^c]*$") },
{ name = luap, code = "ob()", ob = Lua_stringfind('this is ama(231c)zing', "[^c]*$") }
}
},
{
title = [[Non greedy matching v:'(.{-})' l:'%(.-%)' 'this is (ama(231)zing)']],
count = 1000000,
list = {
{ name = vimr, code = "ob()", ob = Vim_stringfind('this is (ama(231)zing)', [[(.\{-})]]) },
{ name = luap, code = "ob()", ob = Lua_stringfind('this is (ama(231)zing)', [[%(.-%)]]) }
}
},
{
title = [[Only numbers and anchor '^[0-9]+$' in '2131409']],
count = 1000000,
list = {
{ name = vimr, code = "ob()", ob = Vim_stringfind('2131409', [[^[0-9]+$]]) },
{ name = luap, code = "ob()", ob = Lua_stringfind('2131409', [[^[0-9]+$]]) }
}
},
{
title = [[C macro '^[A-Z][A-Z0-9_]+$' in 'SUPER_MACRO1']],
count = 1000000,
list = {
{ name = vimr, code = "ob()", ob = Vim_stringfind('SUPER_MACRO1', [[^[A-Z][A-Z0-9_]+$]]) },
{ name = luap, code = "ob()", ob = Lua_stringfind('SUPER_MACRO1', [[^[A-Z][A-Z0-9_]+$]]) }
}
},
{
title = [[Python double underscore '^__[a-zA-Z0-9_]*__$' in '__await1__']],
count = 1000000,
list = {
{ name = vimr, code = "ob()", ob = Vim_stringfind('__await1__', [[^__[a-zA-Z0-9_]*__$]]) },
{ name = luap, code = "ob()", ob = Lua_stringfind('__await1__', [[^__[a-zA-Z0-9_]*__$]]) }
}
},
})
@muniter
Copy link
Author

muniter commented Nov 18, 2021

So I was curious about Lua vs Vim regex/patterns and did some benchmark, non scientific but seems like Lua patterns are well as expected super fast.

NVIM v0.6.0-dev+631-ga6788a979
Build type: Debug
LuaJIT 2.1.0-beta3

Ryzen 7 2700X
Linux Kernel 5.14.16

The benchmars was run as: nvim -u NONE -i NONE bench.lua and then just :source

Benchmark Simple substring zi in this is amazing n: 10000000

Vim regex      : 1.217601
Lua string.find: 0.002445

Benchmark Simple pattern l:%d%d%d%l v:\d\d\d\l this is ama(231c)zing n: 1000000

Vim regex      : 1.275099
Lua string.find: 0.158496

Benchmark Greedy star 2.* this is ama(231c)zing n: 1000000

Vim regex      : 1.265414
Lua string.find: 0.140929

Benchmark Greedy with sets and anchored [^c]*$ this is ama(231c)zing n: 1000000

Vim regex      : 2.517587
Lua string.find: 1.219898

Benchmark Non greedy matching v:(.{-}) l:%(.-%) this is (ama(231)zing) n: 1000000

Vim regex      : 1.20204
Lua string.find: 0.172419

Benchmark Only numbers and anchor ^[0-9]+$ in 2131409 n: 1000000

Vim regex      : 0.458382
Lua string.find: 0.102766

Benchmark C macro ^[A-Z][A-Z0-9_]+$ in SUPER_MACRO1 n: 1000000

Vim regex      : 0.562329
Lua string.find: 0.13639

Benchmark Python double underscore ^__[a-zA-Z0-9_]*__$ in __await1__ n: 1000000

Vim regex      : 1.466258
Lua string.find: 0.180052

Took the benchmark setup from http://lua-users.org/wiki/ObjectBenchmarkTests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment