Mike Lee
Mike Lee

Reputation: 121

Performance difference between DirectoryStream and File.list() in travesing folder

Is there any performance difference between DirectoryStream and File.list()

I have tried to strace both java program in Linux platform, it makes use of getdents64 system call with same parameters. It looks to me both have same performance but different programming paradigm

Upvotes: 3

Views: 2507

Answers (3)

IgnisFatuus
IgnisFatuus

Reputation: 2238

The performance benefit of DirectoryStream come in the form of memory usage and the ability handle the returned path objects as the directory is being listed rather than building the full list and storing it in memory, then iterating over it. This is beneficial when listing directories containing large numbers of files, or when recursively walking a directory tree.

More info here: http://blog.eyallupu.com/2011/11/java-7-working-with-directories.html

Upvotes: 2

user207421
user207421

Reputation: 310875

If they both call the same system call why would there be a difference? Traversing a directory is fundamentally I/O bound.

Upvotes: 1

fstamour
fstamour

Reputation: 799

You might find this interesting:

Mapping java.io.File Functionality to java.nio.file

Upvotes: 2

Related Questions