Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I compared the execution time for unix here-strings and pipe data input to bc:

pipe

time for i in {1..1000}
  do
    echo "sqrt(5.09)" | bc -q > /dev/null
  done


real    0m3.584s
user    0m0.899s
sys     0m2.404s

vs

here-strings

time for i in {1..1000}
  do
    bc -q <<< "sqrt(5.09)" > /dev/null
  done

real    0m5.137s
user    0m0.686s
sys     0m2.262s

(these values are average: tested for many times)

So the real execution time with using here-strings is bigger then using the pipes, but the sum of the user + sys times is still less in the case of here-strings as I initially expected (echo ... | is executed in a new process, so there is an execution overhead). Why such a strange behaviour of bash?

share|improve this question

Not an answer, but an observation: I don't see the same results

$ time for i in {1..10000}
>   do
>     echo "sqrt(5.09)" | bc -q > /dev/null
>   done

real    0m46.786s
user    0m1.584s
sys     0m8.757s

$ time for i in {1..10000}
>   do
>     bc -q <<< "sqrt(5.09)" > /dev/null
>   done

real    0m22.029s
user    0m2.084s
sys     0m3.932s

Also

$ echo $BASH_VERSION
4.2.10(1)-release
share|improve this answer
    
interesting, do you have always the same ratio between rp and rh, up and uh, sp and sh (rp=real,pipe; rh=real,here-string; up=user,pipe; etc.)? – static Jun 2 '13 at 14:03
    
@static same here, the pipe is faster than the here-string for me as well. – terdon Jun 2 '13 at 15:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .