hjohn at xs4all.nl
Fri Sep 4 17:11:46 PDT 2009
I noticed that using a FileVisitor to iterate over a single directory is
far faster than doing a DirectoryStream + readBasicFileAttributes
Results for 9000 file directory:
Simple DirectoryStream iteration = 300 ms
DirectoryStream iteration + readBasicFileAttributes for each entry = 9000 ms
FileVisitor which skips all subdirectories (but does return
BasicFileAttributes for each entry) = 480 ms
Using a FileVisitor to iterate over a single directory seems somewhat
clumsy so I looked at the implementation to see if there was a better
way, but I found that it is basically cheating (Path seems to be an
instance of BasicFileAttributesHolder which obviously is a lot faster
than doing your own Attributes.readBasicFileAttributes(path) call).
I guess what I'm saying is that I didn't really expect that -- I would
have expected that for reading a single directory (+ attributes) there
would be a simple way to do it like DirectoryStream currently provides.
Currently, I think that many would fall for the trap of iterating over a
DirectoryStream and calling readBasicFileAttributes on each entry which
is very slow. Of course now that I figured this out it is no real
problem to just wrap a FileVisitor in my own class to read a single
I hope this feedback is useful.
More information about the nio-dev