Why does Directory.GetFiles() run faster on subseq

2020-03-25 07:05发布

问题:

I'm not really sure what causes this so please forgive me if I couldn't find the information I needed in a search. Here is an example:

Let's say that we have a folder with 1,000,000 files. Running Directory.GetFiles() on that will take a few minutes. However, running it again right after will take only a few seconds. Why does this happen? Are the objects being cached somewhere? How can I run it with the original time?

回答1:

Hard drives have internal caches that will help speed up subsequent reads. Try reading a bunch of other directory information in a completely different sector to clear the cache.