unix – grep如何运行这么快?

我真的很惊讶于GREP在shell中的功能,早期我曾经在java中使用substring方法,但现在我使用GREP为它,它执行在几秒钟,它的速度比我用来写的java代码快得多。 (根据我的经验,我可能是错了)

这是说我没有能够弄清楚它是怎么回事?网上也没有太多可用的。

谁能帮我这个?

假设你的问题具体关注GNU grep。这里是作者Mike Haertel的一个注释:

GNU grep is fast because it AVOIDS LOOKING AT EVERY INPUT BYTE.

GNU grep is fast because it EXECUTES VERY FEW INSTRUCTIONS FOR EACH
BYTE that it
does look at.

GNU grep uses the well-known Boyer-Moore algorithm,which looks first
for the final letter of the target string,and uses a lookup table to
tell it how far ahead it can skip in the input whenever it finds a
non-matching character.

GNU grep also unrolls the inner loop of Boyer-Moore,and sets up the
Boyer-Moore delta table entries in such a way that it doesn’t need to
do the loop exit test at every unrolled step. The result of this is
that,in the limit,GNU grep averages fewer than 3 x86 instructions
executed for each input byte it actually looks at (and it skips many
bytes entirely).

GNU grep uses raw Unix input system calls and avoids copying data
after reading it. Moreover,GNU grep AVOIDS BREAKING THE INPUT INTO
LINES. Looking for newlines would slow grep down by a factor of
several times,because to find the newlines it would have to look at
every byte!

So instead of using line-oriented input,GNU grep reads raw data into
a large buffer,searches the buffer using Boyer-Moore,and only when
it finds a match does it go and look for the bounding newlines
(Certain command line options like
-n disable this optimization.)

这个答案是取自here的信息的子集。

相关文章

普通模式 >G 增加当前行到文档末尾处的缩紧层级 $ 移动到本行的末尾 . 相当于一个...
原文连接: https://spacevim.org/cn/layers/lang/elixir/ 模块简介 功能特性 启用模块 快捷键 语言专属...
原文连接: https://spacevim.org/cn/layers/lang/dart/ 模块简介 功能特性 依赖安装及启用模块 启用模...
 =   赋值操作符,可以用于算术和字符串赋值 +        加法计算     -        减法运算...
1.根据包名来查看指定的APP指定数据 adb shell "top | grep com.xxx.xxx" 由于这样打印出来的数...
ctrl+F 向下翻页 ctrl+B 向下翻页 u 取消最近一次操作 U 取消当前行的操作 ZZ 保存当前内容并退出 gg 跳...