3

I'm curious whether there are some distinguishing performance differences with different filtering orders when using GNU find.

For example, I hope to search all directories in current path whose names match *manifest*.

Will there be any differences internally for the two commands below? That is to say, will the order matter?

find -type d -iname "*manifest*"

or

find -iname "*manifest*" -type d

Note: I care about the performance difference since the path contains lots of files.

5
  • For GNU find have a look at -D opt and -Olevel – Hauke Laging Jan 29 '16 at 8:15
  • @HaukeLaging thanks for telling the 2 options. so according to this, GNU find does do different things as to different predication orders, right? The performance is undecidable, and only through profiling can we get a real fast find result? – Hongxu Chen Jan 29 '16 at 8:37
  • Both -type and -iname are test expressions – Pandya Jan 29 '16 at 8:38
  • @Pandya does GNU find internally distinguish test orders? – Hongxu Chen Jan 29 '16 at 8:39
  • gfind is known to violate POSIX rules when it tries to optimize stat() usage and (in case that the nonstandard primary -noleaf was not specified) makes the illegal assumption that the number of hardlinks for a directory is 2+number-of-subdirs. It is however unlikely that gfind ignores the order of the rules, so it is expected that it helps to first specify the simple tests in a list. – schily Jan 29 '16 at 10:45
3

The syntax for find is:

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

In your case -iname and -type are both expressions. So, there is no problem with using one before another.

From Description:

GNU find searches the directory tree rooted at each given file name by evaluating the given expression from left to right, according to the rules of precedence (see section OPERATORS), until the outcome is known (the left hand side is false for and operations, true for or),at which point find moves on to the next file name.

  • For find -type d -iname "*manifest*": It first test for directory only and then test the name matches with "*manifest*".
  • For find -iname "*manifest*" -type d: It first test the name matches with "*manifest*" and then test directory only.

And executing different orders may result in performance difference for a huge finding.

And in order to optimize, find provides Optimization options as follows:

-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.

In order to analyse the optimization with your current command-line syntax, you can send it for debugging with -D and get optimized command-line.

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

Finally find -D opt -type d -iname "*manifest*" outputs:

Optimized command line:
 ( -iname *manifest* [0.8] -a [0.4] [need type] -type d [0.4]  ) 
3
  • 1
    i'm asking about the potential performance differences, not the output diffrences. – Hongxu Chen Jan 29 '16 at 8:52
  • all (directory and files), is it for per directory or from the root directory? – Hongxu Chen Jan 29 '16 at 8:58
  • 1
    It seems this answer may be not what you're looking for; come to chat for discussion – Pandya Jan 29 '16 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.