为什么定位比查找快?

Sun*_*ahu 7 find locate

我正在我的源中寻找一个文件,并尝试了定位和查找。出人意料的是,locate 是在整个机器中搜索,而且比 find 更快,后者只在当前目录和子目录中搜索。

这是我对两者的 shell 命令:

find . -name vendorsetup.sh # takes 50 seconds to search all files in curren -sub directories.

locate vendorsetup.sh # takes 20 seconds to search file in whole machine.
Run Code Online (Sandbox Code Playgroud)

为什么这个?

Jos*_*Jos 11

locate使用数据库并定期清点您的文件系统。该数据库针对搜索进行了优化。find需要遍历整个子目录,速度相当快,但不如locate快。