aherbert commented on a change in pull request #60: GEOMETRY-75: Performance Module URL: https://github.com/apache/commons-geometry/pull/60#discussion_r374426814
########## File path: commons-geometry-examples/examples-jmh/src/main/java/org/apache/commons/geometry/examples/jmh/euclidean/VectorPerformance.java ########## @@ -0,0 +1,382 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.geometry.examples.jmh.euclidean; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.function.DoubleSupplier; +import java.util.function.Function; +import java.util.function.ToDoubleFunction; +import java.util.function.UnaryOperator; + +import org.apache.commons.geometry.core.Vector; +import org.apache.commons.geometry.euclidean.oned.Vector1D; +import org.apache.commons.geometry.euclidean.threed.Vector3D; +import org.apache.commons.geometry.euclidean.twod.Vector2D; +import org.apache.commons.rng.UniformRandomProvider; +import org.apache.commons.rng.sampling.distribution.ZigguratNormalizedGaussianSampler; +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.Level; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.infra.Blackhole; + +/** + * Benchmarks for the Euclidean vector classes. + */ +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) +@Fork(value = 1, jvmArgs = {"-server", "-Xms512M", "-Xmx512M"}) +public class VectorPerformance { + + /** + * An array of edge numbers that will produce edge case results from functions: + * {@code +/-inf, +/-max, +/-min, +/-0, nan}. + */ + private static final double[] EDGE_NUMBERS = { + Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.MAX_VALUE, + -Double.MAX_VALUE, Double.MIN_VALUE, -Double.MIN_VALUE, 0.0, -0.0, Double.NaN + }; + + /** String constant used to request random double values. */ + private static final String RANDOM = "random"; + + /** String constant used to request a set of double values capable of normalization. */ + private static final String NORMALIZABLE = "normalizable"; + + /** String constant used to request edge-case double values. */ + private static final String EDGE = "edge"; + + /** Base class for vector inputs. + * @param <V> Vector implementation type + */ + @State(Scope.Thread) + public abstract static class VectorInputBase<V extends Vector<V>> { + + /** The dimension of the vector. */ + private final int dimension; + + /** Factory function used to create vectors from arrays of doubles. */ + private final Function<double[], V> vectorFactory; + + /** The number of vectors in the input list. */ + @Param({"1000"}) + private int size; + + /** The vector for the instance. */ + private List<V> vectors; + + /** Create a new instance with the vector dimension. + * @param dimension vector dimension + * @param vectorFactory function for creating vectors from double arrays + */ + VectorInputBase(final int dimension, final Function<double[], V> vectorFactory) { + this.dimension = dimension; + this.vectorFactory = vectorFactory; + } + + /** Set up the instance for the benchmark. + */ + @Setup(Level.Iteration) + public void setup() { + vectors = new ArrayList<>(size); + + final double[] values = new double[dimension]; + final DoubleSupplier doubleSupplier = createDoubleSupplier(getType(), + RandomSource.create(RandomSource.XO_RO_SHI_RO_128_PP)); + + for (int i = 0; i < size; ++i) { + for (int j = 0; j < dimension; ++j) { + values[j] = doubleSupplier.getAsDouble(); + } + + vectors.add(vectorFactory.apply(values)); + } + } + + /** Get the input vectors for the instance. + * @return the input vectors for the instance + */ + public List<V> getVectors() { + return vectors; + } + + /** Get the type of double values to use in the creation of input vectors. + * @return the type of double values to use in the creation of input vectors + */ + public abstract String getType(); + + /** Create a supplier that produces doubles of the given type. + * @param type type of doubles to produce + * @param rng random provider + * @return a supplier that produces doubles of the given type + */ + private DoubleSupplier createDoubleSupplier(final String type, final UniformRandomProvider rng) { + switch (type) { + case RANDOM: + return () -> createRandomDouble(rng); + case NORMALIZABLE: + final ZigguratNormalizedGaussianSampler sampler = ZigguratNormalizedGaussianSampler.of(rng); + return () -> { + double n = sampler.sample(); + return Math.abs(n) == 0 ? 0.1 : n; // do not return exactly zero Review comment: No need for Math.abs here, -0.0 or 0.0 will equal 0. I assume this is to guard against vectors that cannot be normalised as they sum to 0. In this case I would also guard the RANDOM ones since they could also be zero, however unlikely. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
