You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2016/09/04 10:48:04 UTC
[2/2] [math] Usage example (userguide).
Usage example (userguide).
Micro-benchmarking of random number generators ("Commons Rng") using "PerfTestUtils".
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/ae8f5f04
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/ae8f5f04
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/ae8f5f04
Branch: refs/heads/develop
Commit: ae8f5f04574be75727f5e948e04bc649cbdbbb3b
Parents: a215c3f
Author: Gilles <er...@apache.org>
Authored: Sun Sep 4 12:44:06 2016 +0200
Committer: Gilles <er...@apache.org>
Committed: Sun Sep 4 12:44:06 2016 +0200
----------------------------------------------------------------------
.../rng/RandomNumberGeneratorBenchmark.java | 160 +++++++++++++++++++
1 file changed, 160 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/ae8f5f04/src/userguide/java/org/apache/commons/math4/userguide/rng/RandomNumberGeneratorBenchmark.java
----------------------------------------------------------------------
diff --git a/src/userguide/java/org/apache/commons/math4/userguide/rng/RandomNumberGeneratorBenchmark.java b/src/userguide/java/org/apache/commons/math4/userguide/rng/RandomNumberGeneratorBenchmark.java
new file mode 100644
index 0000000..836e41a
--- /dev/null
+++ b/src/userguide/java/org/apache/commons/math4/userguide/rng/RandomNumberGeneratorBenchmark.java
@@ -0,0 +1,160 @@
+/*
+ * 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.math4.userguide.rng;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Random;
+
+import org.apache.commons.rng.RandomSource;
+import org.apache.commons.rng.UniformRandomProvider;
+
+import org.apache.commons.math4.random.RandomGenerator;
+import org.apache.commons.math4.PerfTestUtils;
+
+/**
+ * Benchmark of the RNGs.
+ */
+public class RandomNumberGeneratorBenchmark {
+ /** Number of loops over the operations to be benchmarked. */
+ private static final int NUM_CALLS = 1_000_000;
+ /** Number of runs for computing the statistics. */
+ private static final int NUM_STATS = 500;
+ /** Report formatting. */
+ private static final int MAX_NAME_WIDTH = 45;
+
+ /**
+ * Program's entry point.
+ *
+ * @param args List of {@link RandomSource RNG indentifiers}.
+ */
+ public static void main(String[] args) throws Exception {
+ final RandomNumberGeneratorBenchmark app = new RandomNumberGeneratorBenchmark();
+
+ printEnv();
+
+ final List<UniformRandomProvider> rngList = new ArrayList<>();
+ for (int i = 0; i < args.length; i++) {
+ rngList.add(RandomSource.create(RandomSource.valueOf(args[i])));
+ }
+
+ app.benchmark(rngList);
+ }
+
+ /**
+ * Test all generators.
+ * The reference is JDK's "Random".
+ *
+ * @param list List of generators to benchmark.
+ */
+ public void benchmark(List<UniformRandomProvider> rngList) {
+ // Reference is JDK's "Random".
+ final Random jdk = new Random();
+
+ // List of benchmarked codes.
+ final PerfTestUtils.RunTest[] candidates = new PerfTestUtils.RunTest[rngList.size() + 1];
+
+ // "nextInt()" benchmark.
+
+ candidates[0] = new PerfTestUtils.RunTest(jdk.toString()) {
+ @Override
+ public Double call() throws Exception {
+ return (double) jdk.nextInt();
+ }
+ };
+ for (int i = 0; i < rngList.size(); i++) {
+ final UniformRandomProvider rng = rngList.get(i);
+
+ candidates[i + 1] = new PerfTestUtils.RunTest(rng.toString()) {
+ @Override
+ public Double call() throws Exception {
+ return (double) rng.nextInt();
+ }
+ };
+ }
+
+ PerfTestUtils.timeAndReport("nextInt()",
+ MAX_NAME_WIDTH,
+ NUM_CALLS,
+ NUM_STATS,
+ false,
+ candidates);
+
+ // "nextDouble()" benchmark.
+
+ candidates[0] = new PerfTestUtils.RunTest(jdk.toString()) {
+ @Override
+ public Double call() throws Exception {
+ return (double) jdk.nextDouble();
+ }
+ };
+ for (int i = 0; i < rngList.size(); i++) {
+ final UniformRandomProvider rng = rngList.get(i);
+
+ candidates[i + 1] = new PerfTestUtils.RunTest(rng.toString()) {
+ @Override
+ public Double call() throws Exception {
+ return rng.nextDouble();
+ }
+ };
+ }
+
+ PerfTestUtils.timeAndReport("nextDouble()",
+ MAX_NAME_WIDTH,
+ NUM_CALLS,
+ NUM_STATS,
+ false,
+ candidates);
+
+ // "nextLong()" benchmark.
+
+ candidates[0] = new PerfTestUtils.RunTest(jdk.toString()) {
+ @Override
+ public Double call() throws Exception {
+ return (double) jdk.nextLong();
+ }
+ };
+ for (int i = 0; i < rngList.size(); i++) {
+ final UniformRandomProvider rng = rngList.get(i);
+
+ candidates[i + 1] = new PerfTestUtils.RunTest(rng.toString()) {
+ @Override
+ public Double call() throws Exception {
+ return (double) rng.nextLong();
+ }
+ };
+ }
+
+ PerfTestUtils.timeAndReport("nextLong()",
+ MAX_NAME_WIDTH,
+ NUM_CALLS,
+ NUM_STATS,
+ false,
+ candidates);
+ }
+
+ /**
+ * Print environment.
+ */
+ private static void printEnv() {
+ final String sep = " ";
+ System.out.println("Java" + sep + System.getProperty("java.version"));
+ System.out.println("Runtime" + sep + System.getProperty("java.runtime.version", "?"));
+ System.out.println("JVM" + sep + System.getProperty("java.vm.name") +
+ sep + System.getProperty("java.vm.version"));
+ }
+}