Using Linux

The Execution Time Differences Between sed, grep, tr, and Bash

I’ve been writing a lot of time-sensitive server side code recently and in doing so I’ve come up against the timeless issue of execution times. While there exist many useful and easy-to-understand code frameworks and programs out there, some of the more complex and confusing programs simply run faster than them (most likely due to the lack of “fluff” coding involved in their operation).

The Software Lineup

I decided to perform a simple test of my own to compare the execution times of the following four terminal programs:

sed

sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).

Related information:

grep

grep searches input files for lines containing a match to a given pattern list. When it finds a match in a line, it copies the line to standard output (by default), or produces whatever other sort of output you have requested with options.

Related information:

tr

tr is a command in Unix-like operating systems (and a Bash builtin command). It is an abbreviation of translate or transliterate, indicating its operation of replacing or removing specific characters in its input data set.

Related information:

bash

The following description can be found in the official GNU Bash Reference Manual:

Bash is the shell, or command language interpreter, for the GNU operating system. The name is an acronym for the ‘Bourne-Again SHell’, a pun on Stephen Bourne, the author of the direct ancestor of the current Unix shell sh, which appeared in the Seventh Edition Bell Labs Research version of Unix.

Bash is largely compatible with sh and incorporates useful features from the Korn shell ksh and the C shell csh. It is intended to be a conformant implementation of the IEEE POSIX Shell and Tools portion of the IEEE POSIX specification (IEEE Standard 1003.1). It offers functional improvements over sh for both interactive and programming use.

Related information:

Commands and Results

The test performed compares the operational times of these programs when extracting a simple sub-string of three numbers “999” from a larger string (“I am 999 years old.”). Before and after each respective operation I performed a time stamp with a resolution down to a single nanosecond. The following commands were used:

# sed Test Line
date '+%H:%M:%S %N'; NUMBER=$(echo "I am 999 years old." | sed 's/[^0-9]*//g'); echo $NUMBER; date '+%H:%M:%S %N'

# grep Test Line
date '+%H:%M:%S %N'; NUMBER=$(echo "I am 999 years old." | grep -o -E '[0-9]+'); echo $NUMBER; date '+%H:%M:%S %N'

# tr Test Line
date '+%H:%M:%S %N'; NUMBER=$(echo "I am 999 years old." | tr -dc '0-9'); echo $NUMBER; date '+%H:%M:%S %N'

# bash Test Line
date '+%H:%M:%S %N'; STRING="I am 999 years old."; echo "${STRING//[^0-9]/}"; date '+%H:%M:%S %N'

The results from the test can be seen in the following screenshot:

Terminal program execution times test

By subtracting the start times from the finish times, we can arrive at the desired total execution time. The final results are as follows:

sed
- Start:  21:28:02.848306883
- Finish: 21:28:02.855791549
- Total:         0.007484666

grep
- Start:  21:28:11.268661828
- Finish: 21:28:11.273837048
- Total:         0.005175220

tr
- Start:  21:28:16.657568471
- Finish: 21:28:16.664353744
- Total:         0.006785273

bash
- Start:  21:28:23.424875539
- Finish: 21:28:23.427066077
- Total:         0.002190538

Conclusion

Based on the results of this test, the following general conclusions can be drawn:

Bash operations are…

  • 236% faster than grep
  • 309% faster than tr
  • 341% faster than sed

So the next time you are tempted to go with a simple tr or grep operation, think twice if time is of the essence. Learning a few parameter expansions may save you a lot of time in the long run (even if that only boils down to a few microseconds per cycle).

Published by

willinspire

Entrepreneur, Crypto Trader, Blockchain Consultant, Published Model, Photographer, Anarchist, Visionary

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s