Created
May 20, 2025 07:41
-
-
Save monkoose/8931f5e43d935da2c387650079826b8b to your computer and use it in GitHub Desktop.
trim benchmark
This file contains hidden or 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
| 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