You came to this page from the summary. The page is auto-generated by the Makefile at 2025-07-14 16:29.
package org.eolang.benchmark; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; import java.util.stream.LongStream; import java.util.stream.Stream; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.Fork; import org.openjdk.jmh.annotations.Measurement; import org.openjdk.jmh.annotations.Mode; import org.openjdk.jmh.annotations.OutputTimeUnit; import org.openjdk.jmh.annotations.Scope; import org.openjdk.jmh.annotations.State; import org.openjdk.jmh.annotations.Warmup; @BenchmarkMode({Mode.AverageTime}) @OutputTimeUnit(TimeUnit.MILLISECONDS) @Warmup(iterations = 5, time = 100, timeUnit = TimeUnit.MILLISECONDS) @Measurement(iterations = 10, time = 100, timeUnit = TimeUnit.MILLISECONDS) @State(Scope.Benchmark) @Fork(2) public class Big { private static final Object[] VALUES; static { VALUES = LongStream.range(0L, 10000000L).boxed().map(x -> String.format("%04x", new Object[] { x })).toArray(); } @Benchmark public long plain() { long acc = 0L; for (int idx = 0; idx < VALUES.length; idx++) { String str = ((String)VALUES[idx]).trim(); if (str.length() == 4) acc += Long.parseLong(str, 16) + 1L; } return acc; } @Benchmark public long streams() { return Stream.<Object>of(VALUES) .map(obj -> (String)obj) .map(String::trim) .filter(str -> (str.length() == 4)) .map(str -> Long.valueOf(Long.parseLong(str, 16) + 1L)) .mapToLong(num -> num.longValue()) .sum(); } @Benchmark public long prefused() { return Stream.<Object>of(VALUES) .mapMulti((item, then) -> then.accept(step1(item))) .mapMulti((item, then) -> then.accept(step2(item))) .mapMulti((item, then) -> { if (!step3(item)) return; then.accept(item); }).mapMulti((item, then) -> { long i = step4(item); then.accept(Long.valueOf(i)); }).mapToLong(num -> num.longValue()) .sum(); } @Benchmark public long fused1() { return Stream.<Object>of(VALUES) .mapMulti((item, then) -> then.accept(step2(step1(item)))) .mapMulti((item, then) -> { if (!step3(item)) return; then.accept(item); }).mapMulti((item, then) -> { long i = step4(item); then.accept(Long.valueOf(i)); }).mapToLong(num -> num.longValue()) .sum(); } @Benchmark public long fused2() { return Stream.<Object>of(VALUES) .mapMulti((item, then) -> { String i = step2(step1(item)); if (!step3(i)) return; then.accept(i); }).mapMulti((item, then) -> { long i = step4(item); then.accept(Long.valueOf(i)); }).mapToLong(num -> num.longValue()) .sum(); } @Benchmark public long fused3() { return Stream.<Object>of(VALUES) .mapMulti((item, then) -> { String i1 = step2(step1(item)); if (!step3(i1)) return; long i2 = step4(i1); then.accept(Long.valueOf(i2)); }).mapToLong(num -> num.longValue()) .sum(); } private static String step1(Object item) { return (String)item; } private static String step2(String item) { return item.trim(); } private static boolean step3(String item) { return (item.length() == 4); } private static long step4(String item) { return Long.parseLong(item, 16) + 1L; } }