expand here on our globbing example to illustrate some performance characteristics of the shell script interpreter. Comparing the bash and dash interpreters for this example where a process is spawned for each of 30,000 files, shows that dash can fork the wc processes nearly twice as fast as bash
We'll expand here on our globbing example above to illustrate some performance characteristics of the shell script interpreter. Comparing the
bashanddashinterpreters for this example where a process is spawned for each of 30,000 files, shows that dash can fork thewcprocesses nearly twice as fast asbash
Comparing the base looping speed by not invoking the wc processes, shows that dash's looping is nearly 6 times faster!
Comparing the base looping speed by not invoking the
wcprocesses, shows that dash's looping is nearly 6 times faster!
$ time bash -c 'for i in *; do echo "$i">/dev/null; done'
real 0m1.715s
user 0m1.459s
sys 0m0.252s
$ time dash -c 'for i in *; do echo "$i">/dev/null; done'
$ time dash -c 'for i in *; do echo "$i">/dev/null; done'
real 0m0.375s
user 0m0.169s
sys 0m0.203s
The looping is still relatively slow in either shell as demonstrated previously, so for scalability we should try and use more functional techniques so iteration is performed in compiled processes.
The looping is still relatively slow in either shell as demonstrated previously, so for scalability we should try and use more functional techniques so iteration is performed in compiled processes.
$ time find -type f -print0 | wc -l --files0-from=- | tail -n1
$ time find -type f -print0 | wc -l --files0-from=- | tail -n1
30000 total
real 0m0.299s
user 0m0.072s
sys 0m0.221s
The above is by far the most efficient solution and illustrates the point well that one should do as little as possible in shell script and aim just to use it to connect the existing logic available in the rich set of utilities available on a UNIX system.
The above is by far the most efficient solution and illustrates the point well that one should do as little as possible in shell script and aim just to use it to connect the existing logic available in the rich set of utilities available on a UNIX system.
Stolen From ThisCommon shell script mistakes Pageby Pádraig Brady.