Ubuntu – Cat vs grep vs awk command get the file content which one is more efficient and less time consuming

awkcatgrep

Lets say I have file content like this:

this is a simple file for testing purpose
with few lines in it.
to check the cat and grep command to verfy which is best and less excution time consuming

First I have tried:

time cat temp.txt

Output:

this is a simple file for testing purpose
with few lines in it.
to check the cat and grep command to verfy which is best and less excution time consuming

real    0m0.001s
user    0m0.000s
sys     0m0.001s

Second I have tried:

time grep "$"  temp.txt

Output:

this is a simple file for testing purpose
with few lines in it.
to check the cat and grep command to verfy which is best and less excution time consuming

real    0m0.002s
user    0m0.000s
sys     0m0.002s

Third I have tried:

time awk  "/$/"  temp.txt

Output:

this is a simple file for testing purpose
with few lines in it.
to check the cat and grep command to verfy which is best and less excution time consuming

real    0m0.004s
user    0m0.001s
sys     0m0.004s

With:

time awk 1 temp.txt

Output:

this is a simple file for testing purpose
with few lines in it.
to check the cat and grep command to verfy which is best and less excution time consuming

real    0m0.004s
user    0m0.000s
sys     0m0.003s

With sed:

time sed "" temp.txt

Output:

this is a simple file for testing purpose
with few lines in it.
to check the cat and grep command to verfy which is best and less excution time consuming

real    0m0.002s
user    0m0.000s
sys     0m0.002s

It means cat is quite better command for printing all file content. As it takes less time for execution.?

Best Answer

The answer is "yes". Initially, this is more of an assertion, since cat is merely reading the file, whereas the other two are scanning it for an expression. Your time scripts are the right idea, but at these extremely low durations, any small variance will give erroneous results. Much better to use a larger file, or to repeat it multiple times.

$ time for i in {1..1000}; do cat temp.txt; done
...
real    0m0.762s
user    0m0.060s
sys     0m0.147s

$ time for i in {1..1000}; do grep "$" temp.txt; done
...
real    0m3.128s
user    0m0.667s
sys     0m0.263s

$ time for i in {1..1000}; do awk "/$/" temp.txt; done
...
real    0m3.332s
user    0m0.720s
sys     0m0.337s

Also (not shown), I ran the above commands multiple times to confirm that each command ran at about the same time, and hence was replicable.

More benchmarks

As per the comments, here are some more commands I tested. On my system, grep "^" and awk "1" had no appreciable increase in efficiency, although sed "" approached cat.

$ time for i in {1..1000}; do grep "^" temp.txt; done
...
real    0m2.992s
user    0m0.527s
sys     0m0.303s

$ time for i in {1..1000}; do awk "1" temp.txt; done
...
real    0m3.185s
user    0m0.570s
sys     0m0.317s

$ time for i in {1..1000}; do sed "" temp.txt; done
...
real    0m0.983s
user    0m0.077s
sys     0m0.193s
Related Question