//
// FileOpsTest.java
//

import java.io.File;

/** Tests performance of File.exists() vs File.listFiles(). */
public class FileOpsTest {
  public static void main(String[] args) {
    int iter = 1000;
    if (args.length > 0) iter = Integer.parseInt(args[0]);
    long t1, t2;

    File dot = new File(".");
    File[] list = dot.listFiles();

    // time File.exists with an existing file
    t1 = System.currentTimeMillis();
    for (int i=0; i<iter; i++) list[i % list.length].exists();
    t2 = System.currentTimeMillis();
    System.out.println("File.exists() for existing = " + (t2 - t1) + " ms");

    // time File.exists with a non-existent file
    File non = new File("this-file-is-unlikely-to-exist");
    t1 = System.currentTimeMillis();
    for (int i=0; i<iter; i++) non.exists();
    t2 = System.currentTimeMillis();
    System.out.println("File.exists() for non-exist = " + (t2 - t1) + " ms");

    // time File.listFiles
    t1 = System.currentTimeMillis();
    for (int i=0; i<iter; i++) dot.listFiles();
    t2 = System.currentTimeMillis();
    System.out.println("File.listFiles() = " + (t2 - t1) +
      " ms (" + ((t2 - t1) / list.length) + "/file)");
  }
}

/*
Some results on a MacPro 2 x 2.66 GHz Dual-Core Intel Xeon
Networked file system over SMB on local network (gigabit?)
Windows and Linux results via Parallels v2

LINUX - NETWORK
File.exists() for existing = 16 ms
File.exists() for non-exist = 12 ms
File.listFiles() = 1755 ms (2/file)

MAC OS X - NETWORK
curtis@monk:~/data/perkinelmer/koen/20061107165034$ java FileOpsTest
File.exists() for existing = 58 ms
File.exists() for non-exist = 602 ms
File.listFiles() = 4519 ms (6/file)

WIN XP - NETWORK
Z:\perkinelmer\koen\20061107165034>java -cp C:\svn\java\utils FileOpsTest
File.exists() for existing = 2043 ms
File.exists() for non-exist = 1763 ms
File.listFiles() = 40882 ms (60/file)

LINUX - LOCAL
File.exists() for existing = 10 ms
File.exists() for non-exist = 17 ms
File.listFiles() = 71 ms (2/file)

MAC OS X - LOCAL
curtis@monk:~/svn/java/jar$ java FileOpsTest
File.exists() for existing = 7 ms
File.exists() for non-exist = 12 ms
File.listFiles() = 149 ms (5/file)

WIN XP - LOCAL
C:\svn\java\jar>java -cp ..\utils FileOpsTest
File.exists() for existing = 0 ms
File.exists() for non-exist = 0 ms
File.listFiles() = 91 ms (3/file)
*/

@@ -1681,17 +1681,17 @@
 TWORK%0AZ:
-%5C
+/
 perkinel
@@ -1697,14 +1697,14 @@
 lmer
-%5C
+/
 koen
-%5C
+/
 2006
@@ -1725,26 +1725,26 @@
 a -cp C:
-%5Csvn%5C
+/svn/
 java
-%5C
+/
 utils Fi
@@ -2176,18 +2176,18 @@
 L%0AC:
-%5Csvn%5C
+/svn/
 java
-%5C
+/
 jar%3E
@@ -2201,9 +2201,9 @@
 p ..
-%5C
+/
 util

Fix annoying compile error (I can't have "\u" in a COMMENT? Seriously?).