|
| 1 | +package fastfileindex; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.file.*; |
| 5 | +import java.nio.file.attribute.BasicFileAttributes; |
| 6 | +import java.util.concurrent.atomic.AtomicLong; |
| 7 | + |
| 8 | +public class Benchmark { |
| 9 | + public static void main(String[] args) { |
| 10 | + String path = args.length > 0 ? args[0] : "C:\\"; |
| 11 | + |
| 12 | + System.out.println("=== FastFileIndex vs Java Files.walk() Benchmark ==="); |
| 13 | + System.out.println("Scanning: " + path); |
| 14 | + System.out.println(); |
| 15 | + |
| 16 | + // Warmup |
| 17 | + System.out.println("Warmup runs..."); |
| 18 | + runFastFileIndex(path, true); |
| 19 | + runJavaWalk(path, true); |
| 20 | + System.out.println(); |
| 21 | + |
| 22 | + // Benchmark |
| 23 | + System.out.println("Benchmark runs:"); |
| 24 | + long fastTime = runFastFileIndex(path, false); |
| 25 | + long javaTime = runJavaWalk(path, false); |
| 26 | + |
| 27 | + System.out.println(); |
| 28 | + System.out.println("=== Results ==="); |
| 29 | + System.out.println("FastFileIndex: " + fastTime + " ms"); |
| 30 | + System.out.println("Java Files.walk(): " + javaTime + " ms"); |
| 31 | + |
| 32 | + if (javaTime > 0) { |
| 33 | + double speedup = (double) javaTime / fastTime; |
| 34 | + System.out.printf("Speedup: %.2fx faster%n", speedup); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private static long runFastFileIndex(String path, boolean warmup) { |
| 39 | + if (!warmup) System.out.print("FastFileIndex: "); |
| 40 | + |
| 41 | + long start = System.currentTimeMillis(); |
| 42 | + AtomicLong count = new AtomicLong(0); |
| 43 | + |
| 44 | + FastFileIndex.buildWithProgress(new String[]{path}, new ProgressCallback() { |
| 45 | + @Override |
| 46 | + public void onProgress(String currentFile, long totalFiles, long currentPath) { |
| 47 | + count.incrementAndGet(); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + long end = System.currentTimeMillis(); |
| 52 | + long elapsed = end - start; |
| 53 | + |
| 54 | + if (!warmup) { |
| 55 | + System.out.println(elapsed + " ms (" + count.get() + " files)"); |
| 56 | + } |
| 57 | + |
| 58 | + return elapsed; |
| 59 | + } |
| 60 | + |
| 61 | + private static long runJavaWalk(String path, boolean warmup) { |
| 62 | + if (!warmup) System.out.print("Java Files.walk(): "); |
| 63 | + |
| 64 | + long start = System.currentTimeMillis(); |
| 65 | + AtomicLong count = new AtomicLong(0); |
| 66 | + |
| 67 | + try { |
| 68 | + Files.walk(Paths.get(path)) |
| 69 | + .filter(Files::isRegularFile) |
| 70 | + .forEach(file -> count.incrementAndGet()); |
| 71 | + } catch (IOException e) { |
| 72 | + System.err.println("Error: " + e.getMessage()); |
| 73 | + } |
| 74 | + |
| 75 | + long end = System.currentTimeMillis(); |
| 76 | + long elapsed = end - start; |
| 77 | + |
| 78 | + if (!warmup) { |
| 79 | + System.out.println(elapsed + " ms (" + count.get() + " files)"); |
| 80 | + } |
| 81 | + |
| 82 | + return elapsed; |
| 83 | + } |
| 84 | +} |
0 commit comments