You came to this page from the summary. The page is auto-generated by the Makefile at 2025-07-04 01:41.

package org.eolang.benchmark;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.stream.Collector;
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 ((ArrayList)((ArrayList)((ArrayList)((ArrayList)Stream.<Object>of(VALUES)
      .collect(col((acc, item) -> {
            String i = step1(item);
            acc.add(i);
          }))).stream()
      .collect(col((acc, item) -> {
            String i = step2(item);
            acc.add(i);
          }))).stream()
      .collect(col((acc, item) -> {
            String i = item;
            if (!step3(i))
              return; 
            acc.add(i);
          }))).stream()
      .collect(col((acc, item) -> {
            long i = step4(item);
            acc.add(Long.valueOf(i));
          }))).stream()
      .mapToLong(num -> num.longValue())
      .sum();
  }
  
  @Benchmark
  public long fused1() {
    return ((ArrayList)((ArrayList)((ArrayList)Stream.<Object>of(VALUES)
      .collect(col((acc, item) -> {
            String i = step2(step1(item));
            acc.add(i);
          }))).stream()
      .collect(col((acc, item) -> {
            String i = item;
            if (!step3(i))
              return; 
            acc.add(i);
          }))).stream()
      .collect(col((acc, item) -> {
            long i = step4(item);
            acc.add(Long.valueOf(i));
          }))).stream()
      .mapToLong(num -> num.longValue())
      .sum();
  }
  
  @Benchmark
  public long fused2() {
    return ((ArrayList)((ArrayList)Stream.<Object>of(VALUES)
      .collect(col((acc, item) -> {
            String i = step2(step1(item));
            if (!step3(i))
              return; 
            acc.add(i);
          }))).stream()
      .collect(col((acc, item) -> {
            long i = step4(item);
            acc.add(Long.valueOf(i));
          }))).stream()
      .mapToLong(num -> num.longValue())
      .sum();
  }
  
  @Benchmark
  public long fused3() {
    return ((ArrayList)Stream.<Object>of(VALUES)
      .collect(col((acc, item) -> {
            String i = step2(step1(item));
            if (!step3(i))
              return; 
            long i2 = step4(i);
            acc.add(Long.valueOf(i2));
          }))).stream()
      .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;
  }
  
  private static <T, R> Collector<T, ArrayList<R>, ArrayList<R>> col(BiConsumer<ArrayList<R>, T> fn) {
    return Collector.of(() -> new ArrayList(0), fn, (left, right) -> {
          left.addAll(right);
          return left;
        }new Collector.Characteristics[0]);
  }
}