That is a useless use of cat, which may account for a little (but probably not most) of the lost performance.
I don't know -- I can't see the contents of this myCommand.sh from here. There may well be things that can be improved if you show us what you actually did. Frequent mistakes include the use of cut/awk/sed and so forth many times per loop instead of shell builtins, burdensomely long pipe chains, etc, etc, etc.
In any case, grep is a purpose-built utility made in raw C -- of course it's faster.
the command line "cat myFile.txt | ..." was only for an example and was present in my both tests, for an equality. ^^
For example, here the test 1 :
cat myFile.txt | grep -o "toto"
And here the second test :
cat myFyle.txt | ./test2.sh
with in test2.sh :
while read line
do echo $line | grep -o "toto"
done
So, between those tests, there is the "while" and the "echo" in difference.
Sure, the "echo" imply a lost of performances, but I don't think it's very important. So, it would come from the "while".
Of course, grep is faster because it is written in C, but it is in both case. So it should not change so much...
So, what did I miss ? ^^
---------- Post updated at 02:25 AM ---------- Previous update was at 02:07 AM ----------
So, i made some other tests :
Test 1 :
while read line
do echo $line; grep "coucou"
done
This is so long !
Test2 :
while read line
do echo $line; grep "coucou"
echo $line; grep "hello"
done
Twice longer !
Test 3 :
while read line
do grep "coucou"
done
Almost as quick as a normal grep.
So, my problem would not come from my pipe, but how I use it.
Two solutions :
All of your examples could have been done without cat. you didn't need to use cat to make them "equal".
Of course running grep once is faster than running the same program once for each individual line :wall:
No. Running grep for each individual line has a ton of overhead. You're not supposed to do that.
It doesn't feed the first line into grep at all. You ran grep without a pipe or parameter, so it tries to read from standard input, running grep once, slurping up all the data, causing the loop to break.
Well, it would be. It only runs grep once. Without the pipe, where do you think it's reading from? If not the echo, it's reading from standard input, slurping in all the data in one go, hitting EOF, causing read to fail the next loop, and quitting.
It also ignores the first line because the while loop got to it first.
Please don't take offense, but you have no clue what you're doing. Substituting semicolons for pipes doesn't just make it "faster", it totally alters the meaning of your program. You should learn how to use a shell before complaining that they're slow.
There is a high cost to running and quitting an external shell utility. It has to load and map quite a few files, and for all that work you only feed it a handful of bytes before starting over. This is why constructs like echo single-line | grep | sed | awk | cut are wasteful -- they create and destroy tons of processes per loop, wasting tons of time loading files and writing between pipes that could've been used to do actual work. Imagine only being allowed to say one word per telephone call and calling over and over.
---------- Post updated at 09:49 AM ---------- Previous update was at 09:04 AM ----------
An example of everything that happens when you pipe "echo asdf" into grep. Overhead is red, useful work is green.
It can work really fast once it's loaded, but it takes a bit of work to get there. So it makes sense to run external programs only when you have a reasonable amount of work for them to do.