Skip to content

Instantly share code, notes, and snippets.

@CodingFabian
Last active April 12, 2018 04:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodingFabian/422b6cf4bfcdfece54c1 to your computer and use it in GitHub Desktop.
Save CodingFabian/422b6cf4bfcdfece54c1 to your computer and use it in GitHub Desktop.
Nio vs Io File Absence performance
Apparently the "old" File API is about 3 times as fast here on my mac.
I have not tested it on other OS. Note that the GC stats are misleading:
I got confused why the old File API creates more garbage. It does not.
Check the "_norm" values. Because it has about 3x the iterations,
its total gc time and count is of course higher.
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
// uses counter to ensure we get a cache miss from whatever might be caching
@State(Scope.Thread)
public class FileAccess {
int i = 0;
@Benchmark
public boolean nio() {
return Files.exists(Paths.get("/Users/fabian/work/temp/", "nio" + i++));
}
@Benchmark
public boolean io() {
return new File("/Users/fabian/work/temp/", "io" + i++).exists();
}
}
Benchmark Mode Cnt Score Error Units
FileAccess.io thrpt 20 243762.181 ± 11680.500 ops/s
FileAccess.io:·gc.alloc.rate thrpt 20 182.197 ± 8.744 MB/sec
FileAccess.io:·gc.alloc.rate.norm thrpt 20 784.005 ± 0.010 B/op
FileAccess.io:·gc.churn.PS_Eden_Space thrpt 20 180.112 ± 16.788 MB/sec
FileAccess.io:·gc.churn.PS_Eden_Space.norm thrpt 20 774.989 ± 62.758 B/op
FileAccess.io:·gc.churn.PS_Survivor_Space thrpt 20 0.109 ± 0.031 MB/sec
FileAccess.io:·gc.churn.PS_Survivor_Space.norm thrpt 20 0.472 ± 0.141 B/op
FileAccess.io:·gc.count thrpt 20 108.000 counts
FileAccess.io:·gc.time thrpt 20 48.000 ms
FileAccess.nio thrpt 20 63042.097 ± 4388.955 ops/s
FileAccess.nio:·gc.alloc.rate thrpt 20 132.819 ± 10.539 MB/sec
FileAccess.nio:·gc.alloc.rate.norm thrpt 20 2208.994 ± 70.221 B/op
FileAccess.nio:·gc.churn.PS_Eden_Space thrpt 20 134.349 ± 19.286 MB/sec
FileAccess.nio:·gc.churn.PS_Eden_Space.norm thrpt 20 2231.454 ± 262.013 B/op
FileAccess.nio:·gc.churn.PS_Survivor_Space thrpt 20 0.086 ± 0.023 MB/sec
FileAccess.nio:·gc.churn.PS_Survivor_Space.norm thrpt 20 1.429 ± 0.395 B/op
FileAccess.nio:·gc.count thrpt 20 72.000 counts
FileAccess.nio:·gc.time thrpt 20 32.000 ms
# JMH 1.10.1 (released 11 days ago)
# VM invoker: /Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home/jre/bin/java
# VM options: <none>
# Warmup: 5 iterations, 1 s each
# Measurement: 20 iterations, 1 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: FileAccess.io
Result "io":
267340.600 ±(99.9%) 12996.163 ops/s [Average]
(min, avg, max) = (227755.704, 267340.600, 282358.171), stdev = 14966.401
CI (99.9%): [254344.436, 280336.763] (assumes normal distribution)
# JMH 1.10.1 (released 11 days ago)
# VM invoker: /Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home/jre/bin/java
# VM options: <none>
# Warmup: 5 iterations, 1 s each
# Measurement: 20 iterations, 1 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: FileAccess.nio
Result "nio":
77926.363 ±(99.9%) 5559.603 ops/s [Average]
(min, avg, max) = (66773.390, 77926.363, 83652.838), stdev = 6402.447
CI (99.9%): [72366.760, 83485.966] (assumes normal distribution)
# Run complete. Total time: 00:00:50
Benchmark Mode Cnt Score Error Units
FileAccess.io thrpt 20 267340.600 ± 12996.163 ops/s
FileAccess.nio thrpt 20 77926.363 ± 5559.603 ops/s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment