Performance of bash command
Jan 21, 2024
How long does it take to spawn a shell?
On a Linux x64 Hetzner Arch Linux machine, it takes about 7ms:
$ hyperfine — warmup 3 ‘bash -c “echo hello”’ ‘sh -c “echo hello”’ -N
Benchmark 1: bash -c ‘echo hello’
. Time (mean ± σ): 7.3 ms ±. 1.5 ms. [User: 5.1 ms, System: 1.9 ms]
. Range (min … max): 1.7 ms … 9.4 ms. 529 runs
Benchmark 2: sh -c ‘echo hello’
. Time (mean ± σ): 7.2 ms ±. 1.6 ms. [User: 4.8 ms, System: 2.1 ms]
. Range (min … max): 1.5 ms … 9.6 ms. 327 runs
If your intent is to run a single command, starting the shell can take longer than running the command itself. If you’re running many commands in a loop, that gets expensive quickly.