diff options
author | Gregory Anders <greg@gpanders.com> | 2024-11-26 13:56:01 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-26 13:56:01 -0600 |
commit | 99b5ffd688247f25295f3dd06e57c0d9ad85b072 (patch) | |
tree | e68bff0b460dcbf7ff0187b64665dbf92ae13046 /test/benchmark/text_spec.lua | |
parent | e8450ef2368bd2d80149bf98a34571bc92fb2aba (diff) | |
download | rneovim-99b5ffd688247f25295f3dd06e57c0d9ad85b072.tar.gz rneovim-99b5ffd688247f25295f3dd06e57c0d9ad85b072.tar.bz2 rneovim-99b5ffd688247f25295f3dd06e57c0d9ad85b072.zip |
perf(vim.text): use lookup table implementation for hex encoding (#30080)
Co-authored-by: glepnir <glephunter@gmail.com>
Diffstat (limited to 'test/benchmark/text_spec.lua')
-rw-r--r-- | test/benchmark/text_spec.lua | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/test/benchmark/text_spec.lua b/test/benchmark/text_spec.lua new file mode 100644 index 0000000000..9cfeaf765b --- /dev/null +++ b/test/benchmark/text_spec.lua @@ -0,0 +1,52 @@ +describe('vim.text', function() + --- @param t number[] + local function mean(t) + assert(#t > 0) + local sum = 0 + for _, v in ipairs(t) do + sum = sum + v + end + return sum / #t + end + + --- @param t number[] + local function median(t) + local len = #t + if len % 2 == 0 then + return t[len / 2] + end + return t[(len + 1) / 2] + end + + --- @param f fun(t: number[]): table<number, number|string|table> + local function measure(f, input, N) + local stats = {} ---@type number[] + for _ = 1, N do + local tic = vim.uv.hrtime() + f(input) + local toc = vim.uv.hrtime() + stats[#stats + 1] = (toc - tic) / 1000000 + end + table.sort(stats) + print( + string.format( + '\nN: %d, Min: %0.6f ms, Max: %0.6f ms, Median: %0.6f ms, Mean: %0.6f ms', + N, + math.min(unpack(stats)), + math.max(unpack(stats)), + median(stats), + mean(stats) + ) + ) + end + + local input, output = string.rep('😂', 2 ^ 16), string.rep('F09F9882', 2 ^ 16) + + it('hexencode', function() + measure(vim.text.hexencode, input, 100) + end) + + it('hexdecode', function() + measure(vim.text.hexdecode, output, 100) + end) +end) |