From b4e85092d2bb8d2106a05f076275892e8d5360ec Mon Sep 17 00:00:00 2001 From: Shautvast Date: Mon, 28 Aug 2023 20:18:40 +0200 Subject: [PATCH] v1 --- log | 148 ++++++++++++++++++ .../arrays/ArrayBenchmark.java} | 4 +- .../arrays}/Int2dArray.java | 2 +- .../arrays}/IntArray.java | 2 +- .../benchmarks/loops/LoopBenchmark.java | 57 +++++++ .../arrays}/IntArrayTest.java | 2 +- 6 files changed, 210 insertions(+), 5 deletions(-) create mode 100644 log rename src/main/java/com/github/shautvast/{multidim/JmhBenchmark.java => benchmarks/arrays/ArrayBenchmark.java} (98%) rename src/main/java/com/github/shautvast/{multidim => benchmarks/arrays}/Int2dArray.java (91%) rename src/main/java/com/github/shautvast/{multidim => benchmarks/arrays}/IntArray.java (97%) create mode 100644 src/main/java/com/github/shautvast/benchmarks/loops/LoopBenchmark.java rename src/test/java/com/github/shautvast/{multidim => benchmarks/arrays}/IntArrayTest.java (93%) diff --git a/log b/log new file mode 100644 index 0000000..d462005 --- /dev/null +++ b/log @@ -0,0 +1,148 @@ +/Users/Shautvast/Library/Java/JavaVirtualMachines/openjdk-20.0.1/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=64867:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath /Users/Shautvast/dev/MultiDim/target/classes:/Users/Shautvast/.m2/repository/org/openjdk/jmh/jmh-core/1.36/jmh-core-1.36.jar:/Users/Shautvast/.m2/repository/net/sf/jopt-simple/jopt-simple/5.0.4/jopt-simple-5.0.4.jar:/Users/Shautvast/.m2/repository/org/apache/commons/commons-math3/3.2/commons-math3-3.2.jar org.openjdk.jmh.Main com.github.shautvast.benchmarks.loops.LoopBenchmark.* +# JMH version: 1.36 +# VM version: JDK 20.0.1, OpenJDK 64-Bit Server VM, 20.0.1+9-29 +# VM invoker: /Users/Shautvast/Library/Java/JavaVirtualMachines/openjdk-20.0.1/Contents/Home/bin/java +# VM options: -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=64867:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 +# Blackhole mode: compiler (auto-detected, use -Djmh.blackhole.autoDetect=false to disable) +# Warmup: 5 iterations, 10 s each +# Measurement: 5 iterations, 10 s each +# Timeout: 10 min per iteration +# Threads: 1 thread, will synchronize iterations +# Benchmark mode: Average time, time/op +# Benchmark: com.github.shautvast.benchmarks.loops.LoopBenchmark.cStyle + +# Run progress: 0,00% complete, ETA 00:06:40 +# Fork: 1 of 1 +# Warmup Iteration 1: 1786,018 ns/op +# Warmup Iteration 2: 1888,074 ns/op +# Warmup Iteration 3: 1886,316 ns/op +# Warmup Iteration 4: 1883,777 ns/op +# Warmup Iteration 5: 1885,436 ns/op +Iteration 1: 1887,511 ns/op +Iteration 2: 1887,890 ns/op +Iteration 3: 1881,696 ns/op +Iteration 4: 1880,707 ns/op +Iteration 5: 1880,880 ns/op + + +Result "com.github.shautvast.benchmarks.loops.LoopBenchmark.cStyle": + 1883,737 ±(99.9%) 14,017 ns/op [Average] + (min, avg, max) = (1880,707, 1883,737, 1887,890), stdev = 3,640 + CI (99.9%): [1869,720, 1897,753] (assumes normal distribution) + + +# JMH version: 1.36 +# VM version: JDK 20.0.1, OpenJDK 64-Bit Server VM, 20.0.1+9-29 +# VM invoker: /Users/Shautvast/Library/Java/JavaVirtualMachines/openjdk-20.0.1/Contents/Home/bin/java +# VM options: -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=64867:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 +# Blackhole mode: compiler (auto-detected, use -Djmh.blackhole.autoDetect=false to disable) +# Warmup: 5 iterations, 10 s each +# Measurement: 5 iterations, 10 s each +# Timeout: 10 min per iteration +# Threads: 1 thread, will synchronize iterations +# Benchmark mode: Average time, time/op +# Benchmark: com.github.shautvast.benchmarks.loops.LoopBenchmark.enhancedForLoop + +# Run progress: 25,00% complete, ETA 00:05:01 +# Fork: 1 of 1 +# Warmup Iteration 1: 1773,426 ns/op +# Warmup Iteration 2: 3586,275 ns/op +# Warmup Iteration 3: 5343,418 ns/op +# Warmup Iteration 4: 7113,420 ns/op +# Warmup Iteration 5: 8895,506 ns/op +Iteration 1: 10828,846 ns/op +Iteration 2: 12549,052 ns/op +Iteration 3: 14358,842 ns/op +Iteration 4: 16098,254 ns/op +Iteration 5: 17913,231 ns/op + + +Result "com.github.shautvast.benchmarks.loops.LoopBenchmark.enhancedForLoop": + 14349,645 ±(99.9%) 10787,822 ns/op [Average] + (min, avg, max) = (10828,846, 14349,645, 17913,231), stdev = 2801,563 + CI (99.9%): [3561,823, 25137,467] (assumes normal distribution) + + +# JMH version: 1.36 +# VM version: JDK 20.0.1, OpenJDK 64-Bit Server VM, 20.0.1+9-29 +# VM invoker: /Users/Shautvast/Library/Java/JavaVirtualMachines/openjdk-20.0.1/Contents/Home/bin/java +# VM options: -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=64867:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 +# Blackhole mode: compiler (auto-detected, use -Djmh.blackhole.autoDetect=false to disable) +# Warmup: 5 iterations, 10 s each +# Measurement: 5 iterations, 10 s each +# Timeout: 10 min per iteration +# Threads: 1 thread, will synchronize iterations +# Benchmark mode: Average time, time/op +# Benchmark: com.github.shautvast.benchmarks.loops.LoopBenchmark.iterator + +# Run progress: 50,00% complete, ETA 00:03:21 +# Fork: 1 of 1 +# Warmup Iteration 1: 1769,666 ns/op +# Warmup Iteration 2: 3565,897 ns/op +# Warmup Iteration 3: 5353,325 ns/op +# Warmup Iteration 4: 7105,560 ns/op +# Warmup Iteration 5: 8896,662 ns/op +Iteration 1: 10685,108 ns/op +Iteration 2: 12501,131 ns/op +Iteration 3: 14238,345 ns/op +Iteration 4: 16071,297 ns/op +Iteration 5: 17752,151 ns/op + + +Result "com.github.shautvast.benchmarks.loops.LoopBenchmark.iterator": + 14249,606 ±(99.9%) 10779,967 ns/op [Average] + (min, avg, max) = (10685,108, 14249,606, 17752,151), stdev = 2799,523 + CI (99.9%): [3469,640, 25029,573] (assumes normal distribution) + + +# JMH version: 1.36 +# VM version: JDK 20.0.1, OpenJDK 64-Bit Server VM, 20.0.1+9-29 +# VM invoker: /Users/Shautvast/Library/Java/JavaVirtualMachines/openjdk-20.0.1/Contents/Home/bin/java +# VM options: -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=64867:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 +# Blackhole mode: compiler (auto-detected, use -Djmh.blackhole.autoDetect=false to disable) +# Warmup: 5 iterations, 10 s each +# Measurement: 5 iterations, 10 s each +# Timeout: 10 min per iteration +# Threads: 1 thread, will synchronize iterations +# Benchmark mode: Average time, time/op +# Benchmark: com.github.shautvast.benchmarks.loops.LoopBenchmark.stream + +# Run progress: 75,00% complete, ETA 00:01:40 +# Fork: 1 of 1 +# Warmup Iteration 1: 693,098 ns/op +# Warmup Iteration 2: 2506,948 ns/op +# Warmup Iteration 3: 3707,563 ns/op +# Warmup Iteration 4: 4929,731 ns/op +# Warmup Iteration 5: 6242,310 ns/op +Iteration 1: 7212,976 ns/op +Iteration 2: 8328,529 ns/op +Iteration 3: 9825,261 ns/op +Iteration 4: 11371,792 ns/op +Iteration 5: 12182,884 ns/op + + +Result "com.github.shautvast.benchmarks.loops.LoopBenchmark.stream": + 9784,288 ±(99.9%) 7939,705 ns/op [Average] + (min, avg, max) = (7212,976, 9784,288, 12182,884), stdev = 2061,916 + CI (99.9%): [1844,584, 17723,993] (assumes normal distribution) + + +# Run complete. Total time: 00:06:42 + +REMEMBER: The numbers below are just data. To gain reusable insights, you need to follow up on +why the numbers are the way they are. Use profilers (see -prof, -lprof), design factorial +experiments, perform baseline and negative tests that provide experimental control, make sure +the benchmarking environment is safe on JVM/OS/HW level, ask for reviews from the domain experts. +Do not assume the numbers tell you what you want them to tell. + +NOTE: Current JVM experimentally supports Compiler Blackholes, and they are in use. Please exercise +extra caution when trusting the results, look into the generated code to check the benchmark still +works, and factor in a small probability of new VM bugs. Additionally, while comparisons between +different JVMs are already problematic, the performance difference caused by different Blackhole +modes can be very significant. Please make sure you use the consistent Blackhole mode for comparisons. + +Benchmark Mode Cnt Score Error Units +LoopBenchmark.cStyle avgt 5 1883,737 ± 14,017 ns/op +LoopBenchmark.enhancedForLoop avgt 5 14349,645 ± 10787,822 ns/op +LoopBenchmark.iterator avgt 5 14249,606 ± 10779,967 ns/op +LoopBenchmark.stream avgt 5 9784,288 ± 7939,705 ns/op diff --git a/src/main/java/com/github/shautvast/multidim/JmhBenchmark.java b/src/main/java/com/github/shautvast/benchmarks/arrays/ArrayBenchmark.java similarity index 98% rename from src/main/java/com/github/shautvast/multidim/JmhBenchmark.java rename to src/main/java/com/github/shautvast/benchmarks/arrays/ArrayBenchmark.java index f2873b2..41054fc 100644 --- a/src/main/java/com/github/shautvast/multidim/JmhBenchmark.java +++ b/src/main/java/com/github/shautvast/benchmarks/arrays/ArrayBenchmark.java @@ -1,4 +1,4 @@ -package com.github.shautvast.multidim; +package com.github.shautvast.benchmarks.arrays; import org.openjdk.jmh.annotations.*; @@ -8,7 +8,7 @@ import java.util.concurrent.TimeUnit; @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) @Fork(value = 1) -public class JmhBenchmark { +public class ArrayBenchmark { private static final int ROWS = 1000; private static final int COLS = 1000; diff --git a/src/main/java/com/github/shautvast/multidim/Int2dArray.java b/src/main/java/com/github/shautvast/benchmarks/arrays/Int2dArray.java similarity index 91% rename from src/main/java/com/github/shautvast/multidim/Int2dArray.java rename to src/main/java/com/github/shautvast/benchmarks/arrays/Int2dArray.java index 9bc7f2a..38b242f 100644 --- a/src/main/java/com/github/shautvast/multidim/Int2dArray.java +++ b/src/main/java/com/github/shautvast/benchmarks/arrays/Int2dArray.java @@ -1,4 +1,4 @@ -package com.github.shautvast.multidim; +package com.github.shautvast.benchmarks.arrays; /** * Any dimensional int array, stored as an int[] diff --git a/src/main/java/com/github/shautvast/multidim/IntArray.java b/src/main/java/com/github/shautvast/benchmarks/arrays/IntArray.java similarity index 97% rename from src/main/java/com/github/shautvast/multidim/IntArray.java rename to src/main/java/com/github/shautvast/benchmarks/arrays/IntArray.java index 701bee5..fc430c6 100644 --- a/src/main/java/com/github/shautvast/multidim/IntArray.java +++ b/src/main/java/com/github/shautvast/benchmarks/arrays/IntArray.java @@ -1,4 +1,4 @@ -package com.github.shautvast.multidim; +package com.github.shautvast.benchmarks.arrays; /** * Any dimensional int array, stored as an int[] diff --git a/src/main/java/com/github/shautvast/benchmarks/loops/LoopBenchmark.java b/src/main/java/com/github/shautvast/benchmarks/loops/LoopBenchmark.java new file mode 100644 index 0000000..0baf076 --- /dev/null +++ b/src/main/java/com/github/shautvast/benchmarks/loops/LoopBenchmark.java @@ -0,0 +1,57 @@ +package com.github.shautvast.benchmarks.loops; + +import org.openjdk.jmh.annotations.*; + +import java.util.*; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@Fork(value = 1) +public class LoopBenchmark { + + @org.openjdk.jmh.annotations.State(Scope.Thread) + public static class State { + final static int LIST_SIZE = 1000; + List list = new ArrayList<>(LIST_SIZE); + + @Setup(Level.Iteration) + public void doSetup() { + for (int i = 0; i < LIST_SIZE; i++) { + list.add(UUID.randomUUID().toString()); + } + } + } + + @Benchmark + public long cStyle(LoopBenchmark.State state) { + long totalLength = 0; + for (int i = 0; i < State.LIST_SIZE; i++) { + totalLength += state.list.get(i).length(); + } + return totalLength; + } + + @Benchmark + public long iterator(LoopBenchmark.State state) { + long totalLength = 0; + for (Iterator iterator = state.list.iterator(); iterator.hasNext(); ) { + totalLength += iterator.next().length(); + } + return totalLength; + } + + @Benchmark + public long enhancedForLoop(LoopBenchmark.State state) { + long totalLength = 0; + for (String element : state.list) { + totalLength += element.length(); + } + return totalLength; + } + + @Benchmark + public long stream(LoopBenchmark.State state) { + return state.list.stream().mapToLong(String::length).sum(); + } +} diff --git a/src/test/java/com/github/shautvast/multidim/IntArrayTest.java b/src/test/java/com/github/shautvast/benchmarks/arrays/IntArrayTest.java similarity index 93% rename from src/test/java/com/github/shautvast/multidim/IntArrayTest.java rename to src/test/java/com/github/shautvast/benchmarks/arrays/IntArrayTest.java index 3108289..5ecbc1c 100644 --- a/src/test/java/com/github/shautvast/multidim/IntArrayTest.java +++ b/src/test/java/com/github/shautvast/benchmarks/arrays/IntArrayTest.java @@ -1,4 +1,4 @@ -package com.github.shautvast.multidim; +package com.github.shautvast.benchmarks.arrays; import org.junit.jupiter.api.Test;