查找-regex比find |更慢grep的 [英] Find -regex is slower than find | grep

查看:212
本文介绍了查找-regex比find |更慢grep的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用正则表达式查找文件的脚本。代码如下:

  find $ dir | grep$ regex

脚本运行速度太慢,我想优化它。搜索需要一些时间来执行,我想从中获得更好的性能。我已经尝试过这种尝试:
$ b $ pre $ $ code $ find $ dir -regex* $ {regex}。*

由于没有额外的程序来解析正则表达式, b

然而结果是不一样的,令我惊讶的是find | grep命令比find -regex更快(尽管需要更多的系统时间,正如人们所预料的那样)

我已经计算了这个行为:



查找| grep result

  real 0m12.467s 
user 0m2.568s
sys 0m7.260s

查找-regex结果

  real 0m16.778s 
user 0m6.772s
sys 0m6.380s



<你有什么想法为什么找到-regex解决方案比较慢?

解决方案

很可能是因为 grep 和它的正则表达式引擎已经被高度优化了很多年,因为这是它唯一的目的(做一件事,做得好)。我不知道什么样的正则表达式引擎 find 使用,但显然不像 grep 因为这是一个不太常用的次要功能。

另外,如果你对这个文件列表做任何事情,你应该使用更安全的方式来做到这一点。我不认为 grep 可以使用空分隔的输入(虽然它可以输出),所以你应该使用 find [...] regex [...] -print0 即使速度较慢。


I have a script which looks for a file using a regular expression. The code was the following:

find $dir | grep "$regex"

The script run a bit too slow and I want to optimise it. The search takes some time to perform and I would like to get better performance out of it. I've tried this attempt:

find $dir -regex ".*${regex}.*"

I was expecting slightly faster results as no extra process is created to parse the regular expression.

However the result was different and to my astonishment the command "find | grep" is faster than "find -regex" (although it takes more system time, as one would have expected)

I've timed this behaviour:

Find | grep result

real   0m12.467s
user   0m2.568s
sys    0m7.260s

Find -regex result

real   0m16.778s
user   0m6.772s
sys    0m6.380s

Do you have any idea why the find -regex solution is slower?

解决方案

Most likely because grep and its regex engine has been highly optimized over many years, since that's its only purpose ("do one thing and do it well"). I don't know what regex engine find uses, but it's evidently not as highly refined as grep's, probably because it's a less-often-used secondary feature.

Also, if you are doing anything with this file list, you should really use a more whitespace-safe way of doing this. I don't think grep can take null-delimited input (though it can output it), so you should use find [...] -regex [...] -print0 even though it's slower.

这篇关于查找-regex比find |更慢grep的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆