Six months ago, I wrote Performance in shell and with C about converting a Bash project td.sh into a C. During it, I got some numbers, which showed me the performances of shell builtin and C. Almost two years ago, I wrote about sleep command, also talking about the cost of invoking external command.
Last night, suddenly I realized that I could have just given a very simple example by using true, using Bashs time builtin to time the loops:
test | time |
---|---|
for i in {1..10000}; do true; done | 00.049s |
for i in {1..10000}; do /bin/true; done | 11.556s |
The builtin true is 23,484% faster than the external /bin/true, numbers got from looping the following commands for 1000 iterations, also as another example:
test | time |
---|---|
e '(11.556-0.049)/0.049*100' | 0.028s |
bc <<< '(11.556-0.049)/0.049*100' | 2.560s |
Note
bc actually returns 23400 if without using scale.
The e above is a shell builtin, from the e.bash project I forked off e, tiny expression evaluator. After I learned about the cost, I was no longer using bc just for simple calculation for floating numbers; but with e, I do calculate floating number again in Bash scripts.
These numbers should be very clear abort using external commands. If you have a long loop and there are lots of uses of external command, then you should really consider rewriting in other programming languages.
0 comments:
Post a Comment