Performance difference between stdin and command line argument
The cat file | command
syntax is considered a Useless Use of Cat
. Of all your options, it takes a performance hit because it has to spawn another process in the kernel. However insignificant this may turn out to be in the big picture, it's overhead the other forms don't have. This has been covered on questions such as: Should I care about unnecessary cats?
Between the other two forms there are virtually no performance differences. STDIN is a special file node that the process has to open and read just like any other. Passing a file name instead of STDIN just makes it open a different file.
The difference would be in what features / flexibility you are looking for.
- Passing the file name to the program would mean the input file was seekable. This may or may not matter to the program but some operations can be sped up if the stream is seekable.
- Knowing the actual input file allows your program to potentially write to it. For example
sed -i
for in-place editing. (Note: since this has to create a new file behind the scenes it's not a performance gain over other redirects but it is a convenience step.) - Using shell redirects gives you the ability to concatenate multiple files or even use process redirection.
sed [exp] < file1 file2
or evensed [exp] < <(grep command)
. Details of this use case can be found on this question: Process substitution and pipe
Given that
command file
just opens the file and from then on works like if it wasstdin
, there's little difference. With shell redirection you just open the file beforehand (shell does,) as opposed to command binary itself.If we're talking about
cat file | command
vs.command <file
, then the latter is preferred. You aren't going to notice significant performance difference between the two, but the former is unnecessarily complicated (extra process and shared memory buffer for the pipe, with limited throughput.) Also, you cannotseek
(change the file pointer position arbitrarily) in a pipe, while you can in an ordinary file. Some commands may use more efficient algorithm whenseek
-ing in the input file is possible.