为什么bash中的here-strings比输入命令要慢?


2

我比较了unix here-strings和pipe数据输入的执行时间 bc

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

这里串

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

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

(这些值是平均值:多次测试)

所以 real 使用here-strings的执行时间比使用管道更大 但总和 user + sys 在我最初期望的here-strings的情况下,时间仍然较少(echo ... |在新进程中执行,因此存在执行开销)。为什么这么奇怪的bash行为?

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.