Shell Script – Optimize Bash Script Processing Speed

performanceshell-script

I was wondering if there are general guidelines for optimizing Bash scripts.

  • For example, it is more convenient to write loops than lines of commands, but is it also faster to process for the system? Example:

    for i in a b c; do echo $i; done
    
    echo a
    echo b
    echo c
    
  • Sometimes people present different solutions for the same problem. For example, sed, cut, awk, and echo are all able to strip digits from a string.
    I was wondering if you can say that the fewer digits code has, the faster it is if you use:

    1. the same command, e.g.

      STRING=abc.def
      echo ${STRING} | sed 's/.def//g'
      echo ${STRING} | sed '$s/....$//'
      
    2. different commands, e.g.

      STRING=abc.def
      echo ${STRING} | cut -d . -f 1
      echo ${STRING} | sed 's/.def//g'
      

Best Answer

Shells do not do any reorganization of the code they get handed, it is just interpreted one line after the other (nothing else does much sense in a command interpreter). Much of the time spent by the shell goes to lexical analysis/parsing/launching the programs called.

For simple operations (like the ones munging strings in the examples at the end of the question) I'd be surprised if the time to load the programs don't swamp any minuscule speed differences.

The moral of the story is that if you really need more speed, you are better off with a (semi)compiled language like Perl or Python, which is faster to run to start with, in which you can write many of the operations mentioned directly and don't have to call out to external programs, and has the option to invoke external programs or call into optimized C (or whatever) modules to do much of the job. That is the reason why in Fedora the "system administration sugar" (GUIs, essentially) are written in Python: Can add a nice GUI with not too much effort, fast enough for such applications, have direct access to system calls. If that isn't enough speed, grab C++ or C.

But do not go there, unless you can prove that the performance gain is worth the loss in flexibility and the development time. Shell scripts are not too bad to read, but I shudder when I remember some scripts used to install Ultrix I once tried to decipher. I gave up, too much "shell script optimization" had been applied.

Related Question