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
$ time dash -c 'for i in *; do wc -l "$i">/dev/null; done'
real 0m14.440s
user 0m3.753s
sys 0m10.329s
$ time bash -c 'for i in *; do wc -l "$i">/dev/null; done'
real 0m24.251s
user 0m8.660s
sys 0m14.871s
Comparing the base looping speed by not invoking the wc processes, 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'
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.
$ 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.
Stolen From This Page