Advertisement
Guest User

Untitled

a guest
Nov 6th, 2023
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. package org.example;
  2.  
  3. import org.openjdk.jmh.annotations.*;
  4. import org.openjdk.jmh.infra.Blackhole;
  5.  
  6. import java.util.Arrays;
  7. import java.util.concurrent.TimeUnit;
  8.  
  9. @BenchmarkMode(Mode.AverageTime)
  10. @OutputTimeUnit(TimeUnit.NANOSECONDS)
  11. @State(Scope.Benchmark)
  12. @Measurement(iterations = 3)
  13. @Fork(1)
  14. @Warmup(iterations = 5, time = 10)
  15. public class Test {
  16. public static int[] gen() {
  17. return new int[]{...};
  18. }
  19.  
  20. int[] array = gen();
  21. Integer[] boxArray = Arrays.stream(array).boxed().toArray(Integer[]::new);
  22. final int TOKEN = 10;
  23.  
  24. @Benchmark
  25. public void testNull() {
  26. Blackhole.consumeCPU(TOKEN);
  27. }
  28.  
  29. @Benchmark
  30. public long test() {
  31. Blackhole.consumeCPU(TOKEN);
  32. return Math.max(
  33. Arrays.stream(array).filter(x -> x < 0).count(),
  34. Arrays.stream(array).filter(x -> x > 0).count());
  35. }
  36.  
  37. @Benchmark
  38. public long testBox() {
  39. Blackhole.consumeCPU(TOKEN);
  40. return Math.max(
  41. Arrays.stream(boxArray).filter(x -> x < 0).count(),
  42. Arrays.stream(boxArray).filter(x -> x > 0).count());
  43. }
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement