Think multi core!

Just another hacking tip.
I normally write little bash scripts that do repeating jobs for me and I wrote a little script (simplified):
for i in `seq 100`; do ./ $i ; done
Very simple you would say. But where is the problem with this? This only uses one CPU core at a time. I normally work on a 2 core machine but I will be buying a 8 core soon. So this script only uses one. So now my script looks like this:
for i in `seq 100`; do ./ $i & done
Can you spot the difference. Now I background the job (&) and the loop continues to run. I will not clog up the process ques as the program only runs
$time ./ 1

real 0m0.034s
user 0m0.025s
sys 0m0.008s

But it makes some difference. No backgrounding:
$ time ./gentest

real 0m2.206s
user 0m1.479s
sys 0m0.727s
And backgrounding
$ time ./gentest

real 0m1.128s
user 0m1.469s
sys 0m0.734s

So even with bash scripts you have to take the increase in core count serious.
Note: You don't need the line terminating ';' anymore as '&' acts as a line terminator too. So
for i in `seq 100`; do ./ $i &; done
Would be wrong

No comments: