为什么我不应该使用查找优化?

why should I not use find optimizations?

我在手册和信息页中阅读了有关 find 命令中优化级别的部分,我不明白为什么我应该 使用最激进的优化级别.

我找到的唯一相关句子是(来自 man find 版本 4.4.2):

Conversely, optimisations that prove to be reliable, robust and effective may be enabled at lower optimisation levels over time.

The findutils test suite runs all the tests on find at each optimisation level and ensures that the result is the same.

如果我理解得很好,它是关于通过 findutils 证明 find 的正确行为,但是,这个测试套件确保所有优化级别都给出相同的结果。

你漏掉了这句话:

The cost-based optimiser has a fixed idea of how likely any given test is to succeed.

这意味着如果您的目录包含非常不典型的内容(例如,很多命名管道和很少的 "regular" 文件),优化器实际上可能 恶化 查询的性能(在这种情况下,假设 -type f-type p 更有可能成功,当相反情况成立时)。在这种情况下,您最好手动优化它,这只有在 -O1-O2.

时才有可能

即使忽略这个问题,基于成本的优化器的固定成本也很难做到正确。涉及多个硬件和软件(硬盘、内核、文件系统),它们都自己进行一些缓存和优化。因此,很难预测不同操作的开销,甚至是相对于彼此的开销(例如,我们知道 readdir(2)stat(2) 便宜,但我们不知道 便宜多少)。这意味着即使假设典型的文件系统内容,基于成本的优化也不能始终保证产生最佳优化。较低的优化级别允许您通过反复试验手动调整查询,如果更费力,这可能更可靠。