Skip to content

Instantly share code, notes, and snippets.

@flrgh
Created June 20, 2024 18:49
Show Gist options
  • Save flrgh/349aecd8ca35d20cf0526ec8b218657c to your computer and use it in GitHub Desktop.
Save flrgh/349aecd8ca35d20cf0526ec8b218657c to your computer and use it in GitHub Desktop.
line width benchmarks
local function old(msg)
return math.max(math.max(unpack(vim.tbl_map(function(line)
return vim.fn.strchars(line)
end, msg))))
end
local max = math.max
local strwidth = vim.api.nvim_strwidth or vim.fn.strchars
local function new(msg)
local width = 0
if msg then
for i = 1, #msg do
width = max(width, strwidth(msg[i]))
end
end
return width
end
-- https://github.com/flrgh/lua-utils/blob/main/lib/flrgh/bench.lua
local bench = require "flrgh.bench"
local saved = _G.print
local fh = assert(io.open("data.txt", "w"))
_G.print = function(s)
if s then fh:write(s) end
fh:write("\n")
end
for _, num_lines in ipairs({ 10, 100, 1000 }) do
for _, max_width in ipairs({ 10, 100, 1000 }) do
local lines = require("table.new")(num_lines, 0)
for i = 1, num_lines do
lines[i] = string.rep("x", math.random(1, max_width))
end
-- ensure at least one line is of max width
lines[math.random(1, num_lines)] = string.rep("x", max_width)
bench.many(
{
count = 1000,
title = ("lines: %s, max width: %s"):format(num_lines, max_width),
warmup = true,
},
{
{
name = "old",
func = old,
},
{
name = "new",
func = new,
},
},
lines
)
end
end
fh:close()
_G.print = saved
+--------------------------------------------+
| lines: 10, max width: 10 (1000 iterations) |
+--------------------------------------------+
| func | total(ms) | avg(us) |
+--------------------------------------------+
| old | 7.542 | 7.542 |
| new | 0.592 | 0.592 |
+--------------------------------------------+
+---------------------------------------------+
| lines: 10, max width: 100 (1000 iterations) |
+---------------------------------------------+
| func | total(ms) | avg(us) |
+---------------------------------------------+
| old | 5.297 | 5.297 |
| new | 1.567 | 1.567 |
+---------------------------------------------+
+----------------------------------------------+
| lines: 10, max width: 1000 (1000 iterations) |
+----------------------------------------------+
| func | total(ms) | avg(us) |
+----------------------------------------------+
| old | 25.95 | 25.95 |
| new | 10.174 | 10.174 |
+----------------------------------------------+
+---------------------------------------------+
| lines: 100, max width: 10 (1000 iterations) |
+---------------------------------------------+
| func | total(ms) | avg(us) |
+---------------------------------------------+
| old | 24.393 | 24.393 |
| new | 6.366 | 6.366 |
+---------------------------------------------+
+----------------------------------------------+
| lines: 100, max width: 100 (1000 iterations) |
+----------------------------------------------+
| func | total(ms) | avg(us) |
+----------------------------------------------+
| old | 42.714 | 42.714 |
| new | 14.168 | 14.168 |
+----------------------------------------------+
+-----------------------------------------------+
| lines: 100, max width: 1000 (1000 iterations) |
+-----------------------------------------------+
| func | total(ms) | avg(us) |
+-----------------------------------------------+
| old | 224.771 | 224.771 |
| new | 90.084 | 90.084 |
+-----------------------------------------------+
+----------------------------------------------+
| lines: 1000, max width: 10 (1000 iterations) |
+----------------------------------------------+
| func | total(ms) | avg(us) |
+----------------------------------------------+
| old | 239.586 | 239.586 |
| new | 64.684 | 64.684 |
+----------------------------------------------+
+-----------------------------------------------+
| lines: 1000, max width: 100 (1000 iterations) |
+-----------------------------------------------+
| func | total(ms) | avg(us) |
+-----------------------------------------------+
| old | 422.562 | 422.562 |
| new | 144.566 | 144.566 |
+-----------------------------------------------+
+------------------------------------------------+
| lines: 1000, max width: 1000 (1000 iterations) |
+------------------------------------------------+
| func | total(ms) | avg(us) |
+------------------------------------------------+
| old | 2290.113 | 2290.113 |
| new | 926.801 | 926.801 |
+------------------------------------------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment