Skip to content

Instantly share code, notes, and snippets.

@amekusa
Last active January 30, 2025 13:21
Show Gist options
  • Select an option

  • Save amekusa/84377fd689c7c6cd05dc26074b5666a5 to your computer and use it in GitHub Desktop.

Select an option

Save amekusa/84377fd689c7c6cd05dc26074b5666a5 to your computer and use it in GitHub Desktop.
Lua: External Command Overheads Comparison
-- git clone https://github.com/amekusa/humb.lua.git humb
local humb = require('humb')
local test = humb:new_test({
rpt = 1000,
digits = 4,
})
do
local os_execute = os.execute
local os_execute_arg = 'echo xxx'
test:case('os.execute', function()
os_execute(os_execute_arg)
end)
end
do
local fn_system = vim.fn.system
local fn_system_arg = 'echo xxx'
test:case('vim.fn.system', function()
fn_system(fn_system_arg)
end)
end
do
local system = vim.system
local system_arg = {'echo', 'xxx'}
test:case('vim.system', function()
system(system_arg):wait()
end)
end
test:run()
test:print()
---- RESULTS ----
-- Repeats: 1000
-- #1: os.execute
-- Elapsed: 0.7508
-- Average: 0.0008
-- #2: vim.fn.system
-- Elapsed: 1.1293
-- Average: 0.0011
-- #3: vim.system
-- Elapsed: 1.0697
-- Average: 0.0011
@amekusa
Copy link
Author

amekusa commented Jan 30, 2025

Conclusion

os.execute() is the fastest.
vim.system() is slightly faster than vim.fn.system().

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