1

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.?

6
  • Note that awk 1 temp.txt should be quicker than awk '/$/' temp.txt since it doesn't have to compare each line to a regex. – glenn jackman Dec 24 '14 at 13:23
  • 1
    I feel compelled to point out that we're on the millisecond scale here. In practical terms, the difference is vanishingly small. en.wikipedia.org/wiki/Program_optimization#When_to_optimize – glenn jackman Dec 24 '14 at 13:24
  • You might also want to benchmark sed "" temp.txt – glenn jackman Dec 24 '14 at 13:26
  • 1
    grep and awk and sed are for processing files. Obviously processing is going to involve more processing power than not processing the file and only outputting its contents as-is. – psusi Dec 24 '14 at 15:32
  • @glennjackman Presumably this is a test scenario for a longer task, but I'm not 100% sure. – Sparhawk Dec 25 '14 at 0:45
3

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
2
  • 1
    To be noted: even if you do (mis)use grep or awk for such things, at least use efficient tests. awk "1" took about a quarter of the time and grep "^" about a third for me. I used dd if=/dev/urandom bs=100M count=10 | base64 > /tmp/blah for the file. – muru Dec 24 '14 at 13:25
  • 1
    @muru I've added more commands. I was actually quite surprised at how little difference they made (on my system at least). – Sparhawk Dec 24 '14 at 13:57
0

I have same scripts . In one I'm using cat and in other it's all AWK.

Here's the first one:

#!/bin/bash


        lines=$(cat /etc/passwd | wc -l)

        for ((i=1 ; i <=$lines ; i++ ))
        do
        user=$(cat /etc/passwd | awk -F : -vi=$i 'NR==i {print $1}')
        uid=$(cat /etc/passwd | awk -F : -vi=$i 'NR==i {print $3}')
        gid=$(cat /etc/passwd | awk -F : -vi=$i 'NR==i {print $4}')
        shell=$(cat /etc/passwd | awk -F : -vi=$i 'NR==i {print $7}')
        echo -e "User is : $user \t Uid is : $uid \t Gid is : $gid \t Shell is : $shell"
        done

Here's the second one:

#!/bin/bash


        lines=$(awk  'END {print NR}' /etc/passwd)

        for ((i=1 ; i <=$lines ; i++ ))
        do
        user=$(awk  -F : -vi=$i 'NR==i {print $1}' /etc/passwd)
        uid=$(awk  -F : -vi=$i 'NR==i {print $3}'  /etc/passwd)
        gid=$(awk  -F : -vi=$i 'NR==i {print $4}'  /etc/passwd)
        shell=$(awk  -F : -vi=$i 'NR==i {print $7}' /etc/passwd)
        echo -e "User is : $user \t Uid is : $uid \t Gid is : $gid \t Shell is : $shell"
        done

The time taken for the first script is as follows ( script with CAT statements):

real    0m0.215s
user    0m0.023s
sys     0m0.238s

For the second script which has only AWK statements, time taken is as follows:

real    0m0.132s
user    0m0.013s
sys     0m0.123s

I think awk processing of file is much faster as compared to calling other external function for reading the files. I would be happy for a discussion on the results.

i think AWK performs better in some cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.