8.1.3 Optimisation Options

The ‘-Olevel’ option sets find’s optimisation level to level. The default optimisation level is 1.

At certain optimisation levels (but not by default), find 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

Currently equivalent to optimisation level 1.

1

This is the default optimisation level and corresponds to the traditional behaviour. Expressions 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.

If you use the ‘-fstype FOO’ predicate and specify a filsystem type ‘FOO’ which is not known (that is, present in /etc/mtab) at the time find starts, that predicate is equivalent to ‘-false’.

3

At this optimisation level, the full cost-based query optimizer is enabled. The order of tests is modified so that cheap (i.e., fast) tests are performed first and more expensive ones are performed 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.

The re-ordering of operations performed by the cost-based optimizer can result in user-visible behaviour change. For example, the ‘-readable’ and ‘-empty’ predicates are sensitive to re-ordering. If they are run in the order ‘-empty -readable’, an error message will be issued for unreadable directories. If they are run in the order ‘-readable -empty’, no error message will be issued. This is the reason why such operation re-ordering is not performed at the default optimisation level.