Skip to content

Instantly share code, notes, and snippets.

@monkoose
Created May 20, 2025 07:41
Show Gist options
  • Select an option

  • Save monkoose/8931f5e43d935da2c387650079826b8b to your computer and use it in GitHub Desktop.

Select an option

Save monkoose/8931f5e43d935da2c387650079826b8b to your computer and use it in GitHub Desktop.
trim benchmark
local function benchmark(func, ...)
func(...)
local start = os.clock()
local result = func(...)
local elapsed = os.clock() - start
print(string.format("Time: %.3f ms", elapsed * 1000))
end
local function trim(str)
return str:match('^%s*(.*%S)') or ''
end
local lpeg = vim.lpeg
local trim_pattern = vim.re.compile("%s* {(%s* %S+)*}")
local function trim2(s)
return lpeg.match(trim_pattern, s)
end
local str = [[
]]
local str2 = " \n \f hello \r \v"
local str3 = [[
hello
]]
benchmark(trim, str)
benchmark(trim2, str)
benchmark(trim, str2)
benchmark(trim2, str2)
benchmark(trim, str3)
benchmark(trim2, str3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment