PeterRip
PeterRip

Reputation: 13

Why WalkingFileTree is faster the second time?

I am using the function Files.walkfiletree() from java.NIO, I am looking over a really big tree so when I run the app the first time (with first time I mean each time I turn on my computer) the app takes some time but the second time is really fast. why? is some cache working? Can I use this in some permanent way?

Upvotes: 1

Views: 92

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533530

When you read data from the filesystem, that information is caching making accessing it again much faster. In some cases 100x faster or more. It caches the data in memory because it is faster.

The simplest solution is to access/load this directory structure before you need it and you will get cached performance. e.g. you can do this on start up.

Another solution is to get a faster SSD. Accessing file structures performs a lot of disk operations to get all the pieces of information. A HDD can do up to 120 IOPS, a cheap SSD can do 40,000 IOPS and a fast SSD can do 250,000 IOPS. This can dramatically reduce the time to load this information.

However, since you cannot control what is in memory, except by accessing it repeatedly, it may be pushed out of the disk cache later.

Upvotes: 2

Related Questions