关于“查找”参数命令的性能

关于“查找”参数命令的性能

我很好奇使用 GNU 时不同的过滤顺序是否存在一些明显的性能差异find

例如,我希望搜索当前路径中名称匹配的所有目录*manifest*

下面的两个命令内部会有什么区别吗?也就是说,顺序重要吗?

find -type d -iname "*manifest*"

或者

find -iname "*manifest*" -type d

注意:我关心性能差异,因为路径包含大量文件。

答案1

的语法find是:

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

在你的情况下-iname-type都是表达式。因此,先使用一个再使用另一个是没有问题的。

来自描述:

GNU find 搜索以每个给定文件名为根的目录树根据优先级规则从左到右评估给定的表达式(请参阅运算符部分),直到知道结果(左侧对于 and 运算为假,对于 or 为真),此时 find 移动到下一个文件名。

  • 对于find -type d -iname "*manifest*":它首先仅测试目录,然后测试名称与"*manifest*".
  • 对于find -iname "*manifest*" -type d:它首先测试名称匹配"*manifest*",然后仅测试目录。

并且执行不同的订单可能会导致巨大的发现的性能差异。

并且为了优化,find提供优化选项如下:

-Olevel
              Enables  query  optimisation.    The find program reorders tests to speed up execution while preserving
              the overall effect; that is, predicates with side effects are not reordered  relative  to  each  other.
              The optimisations performed at each optimisation level are as follows.

              0      Equivalent to optimisation level 1.

              1      This  is  the  default optimisation level and corresponds to the traditional behaviour.  Expres‐
                     sions are reordered so that tests based only on the  names  of  files  (for  example  -name  and
                     -regex) are performed first.

              2      Any  -type  or  -xtype tests are performed after any tests based only on the names of files, but
                     before any tests that require information from the inode.  On many modern versions of Unix, file
                     types  are  returned by readdir() and so these predicates are faster to evaluate than predicates
                     which need to stat the file first.

              3      At this optimisation level, the full cost-based query optimiser is enabled.  The order of  tests
                     is modified so that cheap (i.e. fast) tests are performed first and more expensive ones are per‐
                     formed later, if necessary.  Within each cost band, predicates are evaluated  earlier  or  later
                     according  to whether they are likely to succeed or not.  For -o, predicates which are likely to
                     succeed are evaluated earlier, and for -a, predicates which are likely  to  fail  are  evaluated
                     earlier.

为了使用当前命令行语法分析优化,您可以将其发送以进行调试-D并获取优化的命令行

opt    Prints diagnostic information relating to the optimisation of the expression tree;

最后find -D opt -type d -iname "*manifest*"输出:

Optimized command line:
 ( -iname *manifest* [0.8] -a [0.4] [need type] -type d [0.4]  ) 

相关内容