"find"パラメータコマンドのパフォーマンス情報

GNUを使用するときは、フィルタリング順序によって目立つパフォーマンスの違いがあるかどうか疑問に思いますfind

たとえば、現在のパスで名前が一致するすべてのディレクトリを検索したいとします*manifest*

以下の2つのコマンドの間に内部の違いはありますか?つまり、順序は重要ですか?

find -type d -iname "*manifest*"

または

find -iname "*manifest*" -type d

注:パスに多数のファイルが含まれているため、パフォーマンスの違いが心配です。

ベストアンサー1

構文は次findのとおりです。

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

あなたの場合-iname-type両方の表現です。したがって、最初に1つを使用し、もう1つを使用することに問題はありません。

説明から:

GNU findは、指定された各ファイル名にルートを持つディレクトリツリーを検索します。与えられた式は、優先順位規則に従って左から右に評価されます(演算子のセクションを参照)。、結果がわかるまで(左はand演算の場合はfalse、or演算の場合はtrueです)、その時点でfindは次のファイル名に移動します。

  • For find -type d -iname "*manifest*":まずディレクトリだけをテストしてから"*manifest*"
  • For 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]  ) 

おすすめ記事