Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.example;
- import org.openjdk.jmh.annotations.*;
- import org.openjdk.jmh.infra.Blackhole;
- import java.util.Arrays;
- import java.util.concurrent.TimeUnit;
- @BenchmarkMode(Mode.AverageTime)
- @OutputTimeUnit(TimeUnit.NANOSECONDS)
- @State(Scope.Benchmark)
- @Measurement(iterations = 3)
- @Fork(1)
- @Warmup(iterations = 5, time = 10)
- public class Test {
- public static int[] gen() {
- return new int[]{...};
- }
- int[] array = gen();
- Integer[] boxArray = Arrays.stream(array).boxed().toArray(Integer[]::new);
- final int TOKEN = 10;
- @Benchmark
- public void testNull() {
- Blackhole.consumeCPU(TOKEN);
- }
- @Benchmark
- public long test() {
- Blackhole.consumeCPU(TOKEN);
- return Math.max(
- Arrays.stream(array).filter(x -> x < 0).count(),
- Arrays.stream(array).filter(x -> x > 0).count());
- }
- @Benchmark
- public long testBox() {
- Blackhole.consumeCPU(TOKEN);
- return Math.max(
- Arrays.stream(boxArray).filter(x -> x < 0).count(),
- Arrays.stream(boxArray).filter(x -> x > 0).count());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement