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.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 "^"
andawk "1"
had no appreciable increase in efficiency, althoughsed ""
approachedcat
.