Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.