mlangc commented on code in PR #140:
URL: https://github.com/apache/commons-numbers/pull/140#discussion_r1509950259
##########
commons-numbers-examples/examples-jmh/src/main/java/org/apache/commons/numbers/examples/jmh/core/GcdPerformance.java:
##########
@@ -0,0 +1,236 @@
+package org.apache.commons.numbers.examples.jmh.core;
+
+import org.apache.commons.numbers.core.ArithmeticUtils;
+import org.apache.commons.rng.RestorableUniformRandomProvider;
+import org.apache.commons.rng.simple.RandomSource;
+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.Scope;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.infra.Blackhole;
+
+import java.math.BigInteger;
+import java.util.concurrent.TimeUnit;
+
+@BenchmarkMode(Mode.Throughput)
+@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
+@Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
+@State(Scope.Benchmark)
+@Fork(value = 1, jvmArgs = {"-server", "-Xms512M", "-Xmx512M"})
+public class GcdPerformance {
+ private static final int NUM_PAIRS = 1000;
+ private static final long SEED = 42;
+
+ @State(Scope.Benchmark)
+ public static class Ints {
+ final int[] values;
+
+
+ public Ints() {
+ values = getRandomProvider().ints().filter(i -> i !=
Integer.MIN_VALUE).limit(NUM_PAIRS * 2).toArray();
+ }
+ }
+
+ @State(Scope.Benchmark)
+ public static class Longs {
+ final long[] values;
+
+
+ public Longs() {
+ values = getRandomProvider().longs().filter(i -> i !=
Long.MIN_VALUE).limit(NUM_PAIRS * 2).toArray();
+ }
+ }
+
+ private static RestorableUniformRandomProvider getRandomProvider() {
+ return RandomSource.XO_RO_SHI_RO_128_PP.create(SEED);
+ }
+
+ @Benchmark
+ public void gcdInt(Ints ints, Blackhole blackhole) {
+ for (int i = 0; i < ints.values.length; i += 2) {
Review Comment:
I refactored the whole GCD benchmark and included this change as well. The
difference is minimal, and I'm not sure it's even significant. However, it
seems that if I copy the reference to a local variable, the old GCD
implementation for int is slightly faster then the new one, while without the
copy, it seems to be the other way round. Before recent changes, when `values`
was final, there was no difference between the implementations, so I think that
this might be a benchmarking artifact.
What values do you get if you execute the benchmark?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]