Skip to content

Instantly share code, notes, and snippets.

@sys9kdr
Last active November 23, 2024 12:21
Show Gist options
  • Save sys9kdr/03e0772ac9de8715c701f0a4b7840963 to your computer and use it in GitHub Desktop.
Save sys9kdr/03e0772ac9de8715c701f0a4b7840963 to your computer and use it in GitHub Desktop.
neovimのos特定の速度のベンチ
-- 測定回数
local iterations = 100000
-- ヘルパー関数で時間を計測
local function measure_time(fn)
local start = vim.uv.hrtime()
for _ = 1, iterations do
fn()
end
local duration = vim.uv.hrtime() - start
return duration / 1e6 -- ナノ秒をミリ秒に変換
end
local function detect_with_fn_has()
return vim.fn.has('Linux') == 1
end
-- vim.uv を使用した macOS 検出
local function detect_with_uv_os_uname()
return vim.uv.os_uname().sysname == 'Linux'
end
-- vim.fn.has('mac') の実行時間を測定
--local fn_has_time = measure_time(detect_with_fn_has)
local fn_has_time = measure_time(detect_with_fn_has)
print(detect_with_uv_os_uname())
print(string.format("vim.fn.has('mac'): %.2f ms", fn_has_time))
-- vim.uv を使用した実行時間を測定
local uv_time = measure_time(detect_with_uv_os_uname)
print(detect_with_uv_os_uname())
print(string.format('vim.uv.os_uname: %.2f ms', uv_time))
-- 結果を比較
if fn_has_time < uv_time then
print("vim.fn.has('mac') is faster.")
else
print('vim.uv.os_uname is faster.')
end
@sys9kdr
Copy link
Author

sys9kdr commented Nov 23, 2024

vim.fn.has is faster than vim.uv.os_uname in neovim v 0.11

true
vim.fn.has('mac'): 39.46 ms
true
vim.uv.os_uname: 210.83 ms
vim.fn.has('mac') is faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment