bash - 为什么管道进入 cat 比不管道进入 cat 更快?

标签 bash performance pipe sh cat

我刚刚发现执行 find . 比执行 find 慢。 |猫。这是在我的主目录中执行 time find . 3 次的结果:

First:
real    0m4.385s
user    0m0.546s
sys     0m2.072s

Second:
real    0m4.090s
user    0m0.514s
sys     0m1.798s

Third:
real    0m4.197s
user    0m0.508s
sys     0m1.905s

时间查找。 | cat 反而显着改善了结果:

First:
real    0m2.988s
user    0m0.378s
sys     0m1.649s

Second:
real    0m2.768s
user    0m0.370s
sys     0m1.471s

Third:
real    0m2.768s
user    0m0.370s
sys     0m1.471s

如您所知,找到 . | cat 要快得多。我真的很困惑,cat 唯一做的就是将它的输入复制到它的输出,对吧?我真的不知道为什么会这样,如果有人能告诉我为什么会这样,我会很高兴。

作为记录,这是 find 的输出。 |厕所:

 246646  477986 25198490

谢谢。

最佳答案

find. 的调用本身并不比 find 慢。 |猫。当您将输出打印到标准输出时,它只会变慢。 当您将输出重定向到 /dev/null 时(具体来说,调用 find . >/dev/nullfind . | cat >/dev/null),那么你应该用 | 观察它cat 比没有猫慢。

从这些结果中我想到的唯一结论是,通过让 cat 运行单独的打印进程,find 命令不会因阻止打印到 stdout< 而被阻止,而 cat 作为一个单独的进程运行,因此当它被阻止打印到 stdout 时,find 命令仍在运行,实际上是在查找文件。

关于bash - 为什么管道进入 cat 比不管道进入 cat 更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31101885/

相关文章:

r - 比较期望最大化的时间性能的框架

Snow Leopard Macbook 上的 Eclipse IDE 速度缓慢

bash - 在从管道执行的 bash 脚本中使用 read -p

python /POpen/gpg : Supply passphrase and encryption text both through stdin or file descriptor

regex - sed 查找并替换 fastq 正则表达式

regex - sed 命令在 solaris 中有效,但在 Linux 中无效

Bash 脚本检查用户输入是否匹配特定形式

javascript - 徒手绘制时 iOS SVG 滞后

python - 在进程之间通信时,队列比管道有什么优势?

linux - 用于多重选择和命令输入的 Bash 脚本