Why is the second cp command faster

When I execute cp folder1 folder2 -rf , it takes about 10 minutes for the first time. But when he runs the second command cp folder1 folder3 -rf , it takes about 1 minute. folder1 contains about 100,000 files.

Why is runtime improved a second time?

+7
linux shell
source share
3 answers

This is due to page caching. Run sync ; echo 3 > /proc/sys/vm/drop_caches sync ; echo 3 > /proc/sys/vm/drop_caches to make it slow.

Further reading:

+10
source share

The first time files are read from your hard drive.

The second time the files are read from memory.

Linux, like most operating systems, caches access to files / blocks in memory.

+5
source share

This is because the file is now stored in the memory cache. The first time this command was executed, he had to read the file from disk, which was much slower. This is also important to remember if you want to run any form of test using disk access, for example, by adding "cat MYFILE> / dev / null" before the actual test is run to get consistent results.

+1
source share

All Articles