Skip to content

Instantly share code, notes, and snippets.

@adutra
Created June 22, 2019 16:14
Show Gist options
  • Save adutra/7d6c15a403867c4e6f2d052249ecbc46 to your computer and use it in GitHub Desktop.
Save adutra/7d6c15a403867c4e6f2d052249ecbc46 to your computer and use it in GitHub Desktop.
SpliteratorBenchmark.java
public static class SpliteratorBenchmark {
static double sink;
public static void main(String[] args) throws IOException {
final Path inputPath = createInput();
for (int i = 0; i < 3; i++) {
System.out.println("Start processing JDK stream");
measureProcessing(Files.lines(inputPath));
System.out.println("Start processing fixed-batch stream");
measureProcessing(withBatchSize(Files.lines(inputPath), 500));
}
}
private static Stream<String> withBatchSize(Stream<String> in, int batchSize) {
return stream(batchedSpliterator(in.iterator(), batchSize), true);
}
private static Spliterator<String> batchedSpliterator(Iterator<String> toWrap, int batchSize) {
PagingIterable<String> iterable = new MockPagingIterable<>(toWrap);
return new PagingIterableSpliterator<>(iterable, batchSize);
}
private static void measureProcessing(Stream<String> input) throws IOException {
final long start = System.nanoTime();
try (Stream<String> lines = input) {
final long totalTime = lines.parallel().mapToLong(SpliteratorBenchmark::processLine).sum();
final double cpuTime = totalTime, realTime = System.nanoTime() - start;
final int cores = Runtime.getRuntime().availableProcessors();
System.out.println(" Cores: " + cores);
System.out.format(" CPU time: %.2f s\n", cpuTime / SECONDS.toNanos(1));
System.out.format(" Real time: %.2f s\n", realTime / SECONDS.toNanos(1));
System.out.format("CPU utilization: %.2f%%\n\n", 100.0 * cpuTime / realTime / cores);
}
}
private static long processLine(String line) {
final long localStart = System.nanoTime();
double d = 0;
for (int i = 0; i < line.length(); i++)
for (int j = 0; j < line.length(); j++)
d += Math.pow(line.charAt(i), line.charAt(j) / 32.0);
sink += d;
return System.nanoTime() - localStart;
}
private static Path createInput() throws IOException {
final Path inputPath = Paths.get("input.txt");
try (PrintWriter w = new PrintWriter(Files.newBufferedWriter(inputPath))) {
for (int i = 0; i < 20_000; i++) {
final String text = String.valueOf(System.nanoTime());
for (int j = 0; j < 15; j++) w.print(text);
w.println();
}
}
return inputPath;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment