Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 4F4FF200B61 for ; Tue, 9 Aug 2016 22:48:35 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 4DE61160AAD; Tue, 9 Aug 2016 20:48:35 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 94513160A6B for ; Tue, 9 Aug 2016 22:48:34 +0200 (CEST) Received: (qmail 16136 invoked by uid 500); 9 Aug 2016 20:48:33 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 16126 invoked by uid 99); 9 Aug 2016 20:48:33 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 09 Aug 2016 20:48:33 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 94C89E024E; Tue, 9 Aug 2016 20:48:33 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: erans@apache.org To: commits@commons.apache.org Date: Tue, 09 Aug 2016 20:48:34 -0000 Message-Id: In-Reply-To: <8965984aedbe41df858068a058923989@git.apache.org> References: <8965984aedbe41df858068a058923989@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [2/2] commons-rng git commit: Benchmarking code example. archived-at: Tue, 09 Aug 2016 20:48:35 -0000 Benchmarking code example. Thanks to Artem Barger. Committed with some modifications. Project: http://git-wip-us.apache.org/repos/asf/commons-rng/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-rng/commit/05f49b3d Tree: http://git-wip-us.apache.org/repos/asf/commons-rng/tree/05f49b3d Diff: http://git-wip-us.apache.org/repos/asf/commons-rng/diff/05f49b3d Branch: refs/heads/master Commit: 05f49b3d6580aee3ace055fa379c6e1a705e07b9 Parents: 883b17f Author: Gilles Authored: Tue Aug 9 22:46:54 2016 +0200 Committer: Gilles Committed: Tue Aug 9 22:46:54 2016 +0200 ---------------------------------------------------------------------- .../commons/rng/ComputePiTestPerformance.java | 97 ++++++++++++++++++++ 1 file changed, 97 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-rng/blob/05f49b3d/src/test/java/org/apache/commons/rng/ComputePiTestPerformance.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/rng/ComputePiTestPerformance.java b/src/test/java/org/apache/commons/rng/ComputePiTestPerformance.java new file mode 100644 index 0000000..ce5e47e --- /dev/null +++ b/src/test/java/org/apache/commons/rng/ComputePiTestPerformance.java @@ -0,0 +1,97 @@ +/* + * 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.rng; + +import org.openjdk.jmh.annotations.*; +import java.util.concurrent.TimeUnit; + +/** + * Benchmark that compares the performance of the RNG implementations + * by running a simple workload: computation of \( \pi \). + * + * The computation estimates the value by computing the probability that + * a point p=(x, y) will lie in the circle of radius \( r = 1 \) inscribed + * in the square. + * The probability could be computed by \[ area_{circle} / area_{square} \], + * where \( area_{circle} = \pi * r^2 \) and \( area_{square} = 4 r^2 \). + * Hence, the probability is \( \frac{\pi}{4} \). + * + * The Monte Carlo simulation will produce \( N \) points. + * Defining \( N_c \) as the number of point that satisfy \( x^2 + y^2 <= 1 \), + * we will have \( \frac{N_c}{N} \approx \frac{\pi}{4} \). + */ +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) +@Fork(1) +public class ComputePiTestPerformance { + /** + * The benchmark state (retrieve the various "RandomSource"s). + */ + @State(Scope.Benchmark) + public static class Sources { + /** + * RNG providers. + */ + @Param({"JDK", + "WELL_512_A", + "WELL_1024_A", + "WELL_19937_A", + "WELL_19937_C", + "WELL_44497_A", + "WELL_44497_B", + "MT", + "ISAAC", + "SPLIT_MIX_64", + "XOR_SHIFT_1024_S", + "TWO_CMRES", + "MT_64" }) + String randomSourceName; + + /** + * Number of 2D-points to generate. + */ + @Param({"1000000"}) + long numPoints; + + UniformRandomProvider provider; + + @Setup + public void setup() { + final RandomSource randomSource = RandomSource.valueOf(randomSourceName); + provider = RandomSource.create(randomSource); + } + } + + @Benchmark + public double computePi(Sources data) { + long numPointsInCircle = 0; + for (int i = 0; i < data.numPoints; i++) { + final double x = data.provider.nextDouble(); + final double y = data.provider.nextDouble(); + final double r2 = x * x + y * y; + if (r2 <= 1) { + ++numPointsInCircle; + } + } + + final double pi = 4 * numPointsInCircle / (double) data.numPoints; + // System.out.println("pi=" + pi); + return pi; + } +}