Shell loops are slow and bash's are the slowest.
Shells aren't meant to do heavy work in loops. Shells are meant to launch a few external, optimized processes on batches of data.
Anyway, I was curious how shell loops compare so I made a little benchmark:
#!/bin/bash
export IT=$((10**6))
echo POSIX:
for sh in dash bash ksh zsh; do
TIMEFORMAT="%RR %UU %SS $sh"
time $sh -c 'i=0; while [ "$IT" -gt "$i" ]; do i=$((i+1)); done'
done
echo C-LIKE:
for sh in bash ksh zsh; do
TIMEFORMAT="%RR %UU %SS $sh"
time $sh -c 'for ((i=0;i<IT;i++)); do :; done'
done
G=$((10**9))
TIMEFORMAT="%RR %UU %SS 1000*C"
echo 'int main(){ int i,sum; for(i=0;i<IT;i++) sum+=i; printf("%d\n", sum); return 0; }' |
gcc -include stdio.h -O3 -x c -DIT=$G -
time ./a.out
(
Details:
- CPU: Intel(R) Core(TM) i5 CPU M 430 @ 2.27GHz
- ksh: version sh (AT&T Research) 93u+ 2012-08-01
- bash: GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
- zsh: zsh 5.2 (x86_64-unknown-linux-gnu)
- dash: 0.5.7-4ubuntu1
)
The (abbreviated) results (time per iteration) are:
POSIX:
5.8 µs dash
8.5 µs ksh
14.6 µs zsh
22.6 µs bash
C-LIKE:
2.7 µs ksh
5.8 µs zsh
11.7 µs bash
C:
0.4 ns C
From the results:
If you want a slightly faster shell loop, then if you have the [[
syntax and you want a fast shell loop, you're in an advanced shell and you have the C-like for loop too. Use the C like for loop, then. They can be about 2 times as fast as while [
-loops in the same shell.
- ksh has the fastest
for (
loop at about 2.7µs per iteration
- dash has the fastest
while [
loop at about 5.8µs per iteration
C for loops can be 3-4 decimal orders of magnitude faster. (I heard the Torvalds love C).
The optimized C for loop is 56500 times faster than bash's while [
loop (the slowest shell loop) and 6750 times faster than ksh's for (
loop (the fastest shell loop).
Again, the slowness of shells shouldn't matter much though, because the typical pattern with shells is to offload to a few processes of external, optimized programs.
With this pattern, shells often make it much easier to write scripts with performance superior to python scripts (last time I checked, creating process pipelines in python was rather clumsy).
Another thing to consider is startup time.
time python3 -c ' '
takes 30 to 40 ms on my PC whereas shells take around 3ms. If you launch a lot of scripts, this quickly adds up and you can do very very much in the extra 27-37 ms that python takes just to start. Small scripts can be finished several times over in that time frame.
(NodeJs is probably the worst scripting runtime in this department as it takes about 100ms just to start (even though once it has started, you'd be hard pressed to find a better performer among scripting languages)).
let i++
command is slowing down this bash script? – Alex Jones Aug 13 '16 at 08:59cmd.exe
which does exactly that. – cat Aug 13 '16 at 16:39echo start
,sleep 60
,echo foo
. now run the script with bash, edit it in new window with editor ehich preserves inode (I usedjoe
), and change last line to sayecho bar
instead. then go watch original window - it will saystart
,bar
instead ofstart
,foo
. At least it does for me in Debian Jessie. – Matija Nalis Aug 13 '16 at 21:08cmd.exe
, I last used windows while it was stillcommand.com
. See example for @terdon and try it yourself and share results. strace(8) for me shows bash llseeking and reading again after execing sleep. Now, maybe it doesn't ALWAYS do that, but sometimes at least it does. – Matija Nalis Aug 13 '16 at 21:24echo echo hello >> $0
, and run it. – Michael Homer Aug 14 '16 at 10:28