Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!mx05.eternal-september.org!.POSTED!not-for-mail From: Wojtek Newsgroups: comp.lang.java.programmer Subject: Re: The first 10 files Date: Thu, 14 Mar 2013 03:07:10 -0700 Organization: A noiseless patient Spider Lines: 217 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-15"; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: mx05.eternal-september.org; posting-host="913a58905027d5a3bf7c700d71b3a757"; logging-data="23395"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ix6f7cPmkI0AMghXAYpL0G77Q7lWV4Sg=" X-Newsreader: MesNews/1.08.03.00-gb Cancel-Lock: sha1:GXC/xDNwaTPAHGrzo2+iZR8p5mw= Xref: csiph.com comp.lang.java.programmer:22952 Knute Johnson wrote : > > 300003 files in the directory, almost 1.7GB of files, Windows XP, Java 7 and > it takes 16 ms to run. Somebody else should try this out on their computer > to see if it works as fast. Ok, I'm back :-) I am using WinXP and Java 7, and the directory holds 30,001 32K files, 920MBytes The Code: ---------------------------------------------------- package tester; import java.io.File; import java.io.IOException; import java.nio.file.DirectoryStream; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; public class NewsGroup { public static void main( String[] args ) throws IOException { int maxFiles = 10; System.out.println( "Large File Number Tester" ); if (args[0].equals( "nio" )) nioRun( "C:\\apps\\test", maxFiles ); else if (args[0].equals( "io" )) ioRun( "C:\\apps\\test", maxFiles ); else System.out.println( "NewsGroup io|nio" ); } private static void ioRun( String filePath, int maxFiles ) throws IOException { int i = 1; System.out.println( "IO run" ); long start = System.currentTimeMillis(); File folder = new File( filePath ); File[] listOfFiles = folder.listFiles(); for (File file : listOfFiles) { System.out.println( "" + i + ": " + file.getName() ); if (++i > maxFiles) break; } long stop = System.currentTimeMillis(); System.out.println( "Elapsed: " + (stop - start) + " ms" ); } private static void nioRun( String filePath, int maxFiles ) throws IOException { int i = 1; System.out.println( "NIO run" ); long start = System.currentTimeMillis(); Path dir = FileSystems.getDefault().getPath( filePath ); DirectoryStream stream = Files.newDirectoryStream( dir ); for (Path path : stream) { System.out.println( "" + i + ": " + path.getFileName() ); if (++i > maxFiles) break; } long stop = System.currentTimeMillis(); System.out.println( "Elapsed: " + (stop - start) + " ms" ); } } ---------------------------------------------------- A batch file to run it: ---------------------------------------------------- @echo off java -jar NewsGroup.jar %1 ---------------------------------------------------- And the results: ---------------------------------------------------- C:\apps>run io Large File Number Tester IO run 1: F_000000.jpg 2: F_000001.jpg 3: F_000002.jpg 4: F_000003.jpg 5: F_000004.jpg 6: F_000005.jpg 7: F_000006.jpg 8: F_000007.jpg 9: F_000008.jpg 10: F_000009.jpg Elapsed: 156 ms C:\apps>run io Large File Number Tester IO run 1: F_000000.jpg 2: F_000001.jpg 3: F_000002.jpg 4: F_000003.jpg 5: F_000004.jpg 6: F_000005.jpg 7: F_000006.jpg 8: F_000007.jpg 9: F_000008.jpg 10: F_000009.jpg Elapsed: 140 ms C:\apps>run io Large File Number Tester IO run 1: F_000000.jpg 2: F_000001.jpg 3: F_000002.jpg 4: F_000003.jpg 5: F_000004.jpg 6: F_000005.jpg 7: F_000006.jpg 8: F_000007.jpg 9: F_000008.jpg 10: F_000009.jpg Elapsed: 156 ms C:\apps>run nio Large File Number Tester NIO run 1: F_000000.jpg 2: F_000001.jpg 3: F_000002.jpg 4: F_000003.jpg 5: F_000004.jpg 6: F_000005.jpg 7: F_000006.jpg 8: F_000007.jpg 9: F_000008.jpg 10: F_000009.jpg Elapsed: 219 ms C:\apps>run nio Large File Number Tester NIO run 1: F_000000.jpg 2: F_000001.jpg 3: F_000002.jpg 4: F_000003.jpg 5: F_000004.jpg 6: F_000005.jpg 7: F_000006.jpg 8: F_000007.jpg 9: F_000008.jpg 10: F_000009.jpg Elapsed: 31 ms C:\apps>run nio Large File Number Tester NIO run 1: F_000000.jpg 2: F_000001.jpg 3: F_000002.jpg 4: F_000003.jpg 5: F_000004.jpg 6: F_000005.jpg 7: F_000006.jpg 8: F_000007.jpg 9: F_000008.jpg 10: F_000009.jpg Elapsed: 31 ms C:\apps>run nio Large File Number Tester NIO run 1: F_000000.jpg 2: F_000001.jpg 3: F_000002.jpg 4: F_000003.jpg 5: F_000004.jpg 6: F_000005.jpg 7: F_000006.jpg 8: F_000007.jpg 9: F_000008.jpg 10: F_000009.jpg Elapsed: 78 ms C:\apps> ---------------------------------------------------- So NIO is about 4-5 times faster than IO. The first NIO run looks like an anomoly, might be some JRE loading happening. All the runs produce different timings, might be a Windows caching effect. However the NIO is consistently much faster overall. -- Wojtek :-)