You came to this page from the summary. The page is auto-generated by the Makefile at 2024-11-26 11:19 .

package org.eolang.benchmark;
import java.util.concurrent.TimeUnit;
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 = 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;
  
  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();
  }
}

package org.eolang.benchmark;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
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 = 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;
  
  static {
    VALUES = IntStream.range(0, 100000000).mapToLong(i -> (i % 1000)).toArray();
  }
  
  @Benchmark
  public long plain() {
    long acc = 0L;
    for (int idx = 0; idx < VALUES.length; idx++) {
      if (idx % 2 == 0)
        acc += VALUES[idx] * VALUES[idx]; 
    } 
    return acc;
  }
  
  @Benchmark
  public long streams() {
    return LongStream.of(VALUES)
      .filter(x -> (x % 2L == 0L))
      .map(x -> x * x)
      .sum();
  }
}

package org.eolang.benchmark;
import java.util.concurrent.TimeUnit;
import java.util.function.LongUnaryOperator;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
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 = 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;
  
  static {
    VALUES = IntStream.range(0, 100000000).mapToLong(i -> (i % 1000)).toArray();
  }
  
  @Benchmark
  public long plain() {
    LongUnaryOperator iface = num -> num * num;
    long acc = 0L;
    for (int idx = 0; idx < VALUES.length; idx++)
      acc += iface.applyAsLong(VALUES[idx]); 
    return acc;
  }
  
  @Benchmark
  public long streams() {
    return LongStream.of(VALUES)
      .map(num -> num * num)
      .sum();
  }
}