View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.commons.geometry.examples.jmh;
    18
    19  import org.apache.commons.rng.UniformRandomProvider;
    20
    21  /** Class containing static utility methods for performance benchmarks.
    22   */
    23  public final class BenchmarkUtils {
    24
    25      /** Default min exponent for random double values. */
    26      public static final int DEFAULT_MIN_EXP = -64;
    27
    28      /** Default max exponent for random double values. */
    29      public static final int DEFAULT_MAX_EXP = 64;
    30
    31      /** Utility class; no instantiation. */
    32      private BenchmarkUtils() {}
    33
    34      /** Creates a random double number with a random sign and mantissa and a large, default
    35       * range for the exponent. The numbers will not be uniform over the range.
    36       * @param rng random number generator
    37       * @return the random number
    38       */
    39      public static double randomDouble(final UniformRandomProvider rng) {
    40          return randomDouble(DEFAULT_MIN_EXP, DEFAULT_MAX_EXP, rng);
    41      }
    42
    43      /** Create a random double value with exponent in the range {@code [minExp, maxExp]}.
    44       * @param minExp minimum exponent; must be less than {@code maxExp}
    45       * @param maxExp maximum exponent; must be greater than {@code minExp}
    46       * @param rng random number generator
    47       * @return random double
    48       */
    49      public static double randomDouble(final int minExp, final int maxExp, final UniformRandomProvider rng) {
    50          // Create random doubles using random bits in the sign bit and the mantissa.
    51          final long mask = ((1L << 52) - 1) | 1L << 63;
    52          final long bits = rng.nextLong() & mask;
    53          // The exponent must be unsigned so + 1023 to the signed exponent
    54          final long exp = rng.nextInt(maxExp - minExp + 1) + minExp + 1023;
    55          return Double.longBitsToDouble(bits | (exp << 52));
    56      }
    57
    58      /** Create an array of doubles populated using {@link #randomDouble(UniformRandomProvider)}.
    59       * @param rng uniform random provider
    60       * @param len array length
    61       * @return array containing {@code len} random doubles
    62       */
    63      public static double[] randomDoubleArray(final int len, final UniformRandomProvider rng) {
    64          return randomDoubleArray(len, DEFAULT_MIN_EXP, DEFAULT_MAX_EXP, rng);
    65      }
    66
    67      /** Create an array with the given length containing random doubles with exponents in the range
    68       * {@code [minExp, maxExp]}.
    69       * @param len array length
    70       * @param minExp minimum exponent; must be less than {@code maxExp}
    71       * @param maxExp maximum exponent; must be greater than {@code minExp}
    72       * @param rng random number generator
    73       * @return array of random doubles
    74       */
    75      public static double[] randomDoubleArray(final int len, final int minExp, final int maxExp,
    76              final UniformRandomProvider rng) {
    77          final double[] arr = new double[len];
    78          for (int i = 0; i < arr.length; ++i) {
    79              arr[i] = randomDouble(minExp, maxExp, rng);
    80          }
    81          return arr;
    82      }
    83  }