Short answer
Using multiple expressions will be faster than using multiple pipelines, because you have the additional overhead of creating pipelines and forking sed processes. However, in practice this is rarely the case in practice.
Benchmarks
Using multiple expressions is faster than multiple pipelines, but this is probably not enough for the average use case. Using your example, the average difference in execution speed was only two thousandths of a second, which is not enough to worry.
# Average run with multiple pipelines. $ time { echo "$var1" | sed 's/pattern1/replacement1/g' | sed 's/pattern2/replacement2/g' | sed 's/pattern3/replacement3/g' | sed 's/pattern4/replacement4/g' | sed 's/pattern5/replacement5/g' } Some string of text real 0m0.007s user 0m0.000s sys 0m0.004s
# Average run with multiple expressions. $ time { echo "$var1" | sed \ -e 's/pattern1/replacement1/g' \ -e 's/pattern2/replacement2/g' \ -e 's/pattern3/replacement3/g' \ -e 's/pattern4/replacement4/g' \ -e 's/pattern5/replacement5/g' } Some string of text real 0m0.005s user 0m0.000s sys 0m0.000s
Of course, this is not checking for a large input file, thousands of input files, or running in a loop with tens of thousands of iterations. Nevertheless, it is safe to say that the difference is small enough to be irrelevant for most common situations.
Unusual situations are a completely different story. In such cases, benchmarking will help you determine if replacing pipes with inline expressions is a valuable optimization for this use case.
source share