I've run @Mass bench against another one that avoids :for
as I have the experience of extra costs associated to control statements.
My convoluted solution also does its best to avoid lambdas.
function! lh#time#bench_n2(n, F, ...) abort
if type(a:F) == type(function('type'))
let benchs = map(range(1, a:n), '[reltime()] + [a:F()] + [reltime()]')
else
let benchs = map(range(1, a:n), '[reltime()] + ['.a:F.'] + [reltime()]')
endif
let res = benchs[0][1]
call map(benchs, 'reltimefloat(reltime(v:val[0], v:val[2]))')
let accu = [0]
call map(copy(benchs), 'add(accu, v:val + accu[-1])')
return [res, accu[-1]]
endfunction
With the following on top of Mass' bench
echo lh#time#bench_n2(1000000, "matchstr('strpatingpattern', 'pattern') ")
echo lh#time#bench_n2(1000000, "match('strpatingpattern', 'pattern') ")
echo lh#time#bench_n2(1000000, "'strpatingpattern' =~ 'pattern' ")
echo lh#time#bench_n2(1000000, "'strpatingpattern' =~# 'pattern' ")
In the end, the results are a little bit more stable on my machine -- yet sometimes =~
is a little bit faster than =~#
, sometimes it's the other way around. And also, now I'm sure it's convoluted and not necessary: Mass' simple solution is more than enough.
" Mass' bench in order to have an a idea
5.726717
5.520035
4.839088
" Mine
['pattern', 4.724165]
[9, 4.75635]
[1, 4.313803]
[1, 4.557732]
In conclusion, yeah matchstr()
is a little bit slower than match()
wich is a little bit slower than =~
, but honestly? I doubt you'll ever notice the difference, even when processing 100k or 1M tags. When you need matchstr()
, or match()
or ... extra services, use them. Otherwise use whatever you wish/prefer/like to test whether it matches.