Ubuntu – Why does running the “find” command a second time complete quicker than the first time

bashcommand linefind

When I run time find ~ the first time, meaning the first time I run find in a session, it takes longer than than when run a second time. Why is that?

First run:

real    0m12.410s
user    0m0.424s
sys     0m1.287s

Second run:

real    0m0.988s
user    0m0.214s
sys     0m0.405s

Best Answer

The link provided by Rinzwind titled Why is [find] running incredible fast, if run twice? and What is “cached” in the top command? cover the same issue.

Essentially, if free RAM is available, the kernel can use part of it to cache information. The memory used in this way can be made available to applications on demand.

Linux ate my RAM explains terms such as available, buffer, free, and used. It also provides the following command to clear "most of the disk cache":

echo 3 | sudo tee /proc/sys/vm/drop_caches

To confirm that find is quicker the second time around, I ran time find ~ for the first time:

real    0m10.765s
user    0m0.395s
sys     0m1.169s

And a second time to see the beneficial effect of memory caching:

real    0m1.119s
user    0m0.196s
sys     0m0.476s

Then I cleared cache with echo 3 | sudo tee /proc/sys/vm/drop_caches and ran time find ~ again. Sure enough, the time was high again:

real    0m19.198s
user    0m0.457s
sys     0m1.425s

But, because of fresh caching, a run of find subsequently and expectedly, showed an improved time:

real    0m1.100s
user    0m0.199s
sys     0m0.484s

While reading about cache, I came across Caching/preloading files on Linux into RAM and a couple of answers suggested vmtouch - the Virtual Memory Toucher: it is described as a "Portable file system cache diagnostics and control" and is in the repos for Bionic and later.