跳到主要內容

[tips] Optimize shell scripts

Currently I notice 2 tips to optimize shell scripts. The first is GNU Parallel, which is designed for this purpose for long. For example, if you have the following shell script which runs 3 seconds:


#!/usr/bin/env sh

#Job A
sleep 1
echo 'A done'
#Job B
sleep 1
echo 'B done'
#Job C
sleep 1
echo 'C done'

echo 'All Done'

$time ./seq.sh
A done
B done
C done
All Done

real    0m3.006s
user    0m0.003s
sys     0m0.002s

You can use GNU Parallel to run Job A, B, C simultaneously:

$time (echo 'sleep 1;echo A done'; echo 'sleep 1; echo B done'; echo 'sleep 1; echo C done') | parallel; echo 'All Done'
A done
B done
C done

real    0m1.206s
user    0m0.070s
sys     0m0.063s
All Done

The second one runs with similar idea but with an old tool you might not think of it at the first place, Makefile!!

A:
        @sleep 1
        @echo 'A done'
B:
        @sleep 1
        @echo 'B done'
C:
        @sleep 1
        @echo 'C done'

Done: A B C
        @echo 'All Done'

$time make -j16 Done
A done
B done
C done
All Done

real    0m1.006s
user    0m0.000s
sys     0m0.008s

Cool? :-)

留言