Last active
November 18, 2021 13:08
-
-
Save muniter/9e6d0e86287307df9b980ddce5481a7b to your computer and use it in GitHub Desktop.
Lua patterns and vim regex, not a real benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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_]*__$]]) } | |
} | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The benchmars was run as:
nvim -u NONE -i NONE bench.lua
and then just:source
Benchmark Simple substring
zi
inthis is amazing
n: 10000000Benchmark Simple pattern l:
%d%d%d%l
v:\d\d\d\l
this is ama(231c)zing
n: 1000000Benchmark Greedy star
2.*
this is ama(231c)zing
n: 1000000Benchmark Greedy with sets and anchored
[^c]*$
this is ama(231c)zing
n: 1000000Benchmark Non greedy matching v:
(.{-})
l:%(.-%)
this is (ama(231)zing)
n: 1000000Benchmark Only numbers and anchor
^[0-9]+$
in2131409
n: 1000000Benchmark C macro
^[A-Z][A-Z0-9_]+$
inSUPER_MACRO1
n: 1000000Benchmark Python double underscore
^__[a-zA-Z0-9_]*__$
in__await1__
n: 1000000Took the benchmark setup from http://lua-users.org/wiki/ObjectBenchmarkTests