java - Why is File::listFiles faster than DirectoryStream - accepting only directories -
so "heard" directorystream
introduced in java 7 faster traditional methods of directory listing. however, not case me. might have been faster listing whole directory, when filtering out files , accepting folders, takes far more time. instance, these codes:
using file
:
arraylist<file> temparray = new arraylist(); (file file : somefile.listfiles()){ if(!file.isdirectory()) continue; temparray.add(file); }
using directorystream
:
directorystream<path> stream = null; try { stream = files.newdirectorystream(dir, new directorystream.filter<path>() { public boolean accept(path file) throws ioexception { return files.isdirectory(file); } }); } catch (ioexception ex) { } arraylist<file> files = new arraylist(); (path path : stream) files.add(path.tofile()); stream.close();
the second method (using directorystream) more slower. know both methods check each , every file see if directory. or because directorystream
is synchronized (again, correct me if wrong)?
well, in second case you're testing twice path directory. further, you're defining new class, has relatively large first-use cost.
Comments
Post a Comment