You came to this page from the summary. The page is auto-generated by the Makefile at 2024-11-26 11:19 .
@BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.MILLISECONDS) @Warmup(iterations = 5, time = 10, timeUnit = TimeUnit.MILLISECONDS) @Measurement(iterations = 10, time = 10, timeUnit = TimeUnit.MILLISECONDS) @State(Scope.Benchmark) @Fork(2) public class Big { private static final Object[] VALUES = LongStream.range(0L, 10_000_000L) .boxed() .map(x -> String.format("%04x", x)) .toArray(); @Benchmark public long plain() { long acc = 0L; for (int idx = 0; idx < Big.VALUES.length; idx++) { final String str = ((String) Big.VALUES[idx]).trim(); if (str.length() != 4) { continue; } acc += Long.parseLong(str, 16) + 1L; } return acc; } @Benchmark public long streams() { return Stream.of(Big.VALUES) .map(obj -> (String) obj) .map(String::trim) .filter(str -> str.length() == 4) .map(str -> Long.parseLong(str, 16) + 1L) .mapToLong(num -> num) .sum(); } } @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.MILLISECONDS) @Warmup(iterations = 3, time = 1, timeUnit = TimeUnit.MILLISECONDS) @Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.MILLISECONDS) @Fork(1) @State(Scope.Benchmark) public class Even { private static final long[] VALUES = IntStream.range(0, 100_000_000) .mapToLong(i -> (long) (i % 1000)) .toArray(); @Benchmark public long plain() { long acc = 0L; for (int idx = 0; idx < Even.VALUES.length; idx++) { if (idx % 2 == 0) { acc += Even.VALUES[idx] * Even.VALUES[idx]; } } return acc; } @Benchmark public long streams() { return LongStream.of(Even.VALUES) .filter(x -> x % 2L == 0L) .map(x -> x * x) .sum(); } } @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.MILLISECONDS) @Warmup(iterations = 3, time = 1, timeUnit = TimeUnit.MILLISECONDS) @Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.MILLISECONDS) @Fork(1) @State(Scope.Benchmark) public class Sum { private static final long[] VALUES = IntStream.range(0, 100_000_000) .mapToLong(i -> (long) (i % 1000)) .toArray(); @Benchmark public long plain() { final LongUnaryOperator iface = num -> num * num; long acc = 0L; for (int idx = 0; idx < Sum.VALUES.length ; idx++) { acc += iface.applyAsLong(Sum.VALUES[idx]); } return acc; } @Benchmark public long streams() { return LongStream.of(Sum.VALUES) .map(num -> num * num) .sum(); } }