Skip to content

Commit e28251f

Browse files
committed
Add benchmark comparing FastFileIndex vs Java Files.walk()
1 parent 69909dc commit e28251f

2 files changed

Lines changed: 149 additions & 0 deletions

File tree

examples/Benchmark/pom.xml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>com.github.andrestubbe</groupId>
9+
<artifactId>fastfileindex-benchmark</artifactId>
10+
<version>1.0.0</version>
11+
<packaging>jar</packaging>
12+
13+
<name>FastFileIndex Benchmark</name>
14+
<description>Benchmark comparing FastFileIndex vs Java Files.walk()</description>
15+
16+
<properties>
17+
<maven.compiler.source>17</maven.compiler.source>
18+
<maven.compiler.target>17</maven.compiler.target>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
</properties>
21+
22+
<repositories>
23+
<repository>
24+
<id>jitpack.io</id>
25+
<url>https://jitpack.io</url>
26+
</repository>
27+
</repositories>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>com.github.andrestubbe</groupId>
32+
<artifactId>fastcore</artifactId>
33+
<version>v1.0.0</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>com.github.andrestubbe</groupId>
37+
<artifactId>fastfileindex</artifactId>
38+
<version>v1.0.0</version>
39+
</dependency>
40+
</dependencies>
41+
42+
<build>
43+
<sourceDirectory>src/main/java</sourceDirectory>
44+
<plugins>
45+
<plugin>
46+
<groupId>org.apache.maven.plugins</groupId>
47+
<artifactId>maven-compiler-plugin</artifactId>
48+
<version>3.11.0</version>
49+
<configuration>
50+
<source>17</source>
51+
<target>17</target>
52+
</configuration>
53+
</plugin>
54+
<plugin>
55+
<groupId>org.codehaus.mojo</groupId>
56+
<artifactId>exec-maven-plugin</artifactId>
57+
<version>3.1.0</version>
58+
<configuration>
59+
<mainClass>fastfileindex.Benchmark</mainClass>
60+
<commandlineArgs>--enable-native-access=ALL-UNNAMED</commandlineArgs>
61+
</configuration>
62+
</plugin>
63+
</plugins>
64+
</build>
65+
</project>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)