Search code examples
c#performancecachingexecution

Why does Directory.GetFiles() run faster on subsequent runs?


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?


Solution

  • 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.