view src/main/java/org/openjdk/gcbench/yield/MonteCarloPI.java @ 60:4c32eb6c67b0

More yield tests.
author shade
date Tue, 20 Dec 2016 18:52:31 +0100
parents src/main/java/org/openjdk/gcbench/yield/ArrayIteration.java@5b77fb55a8b6
children 0cb1442be9d6
line wrap: on
line source

package org.openjdk.gcbench.yield;

import org.openjdk.gcbench.tests.UnderPressureTest;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.SECONDS)
@Threads(Threads.MAX)
@State(Scope.Benchmark)
public class MonteCarloPI extends UnderPressureTest {

    @Param({"100000000"})
    int size;

    @Benchmark
    public int test() throws InterruptedException {
        ThreadLocalRandom r = ThreadLocalRandom.current();
        double inside = 0;
        for (int c = 0; c < size; c++) {
            double x = r.nextDouble();
            double y = r.nextDouble();
            if (x*x + y*y < 1) {
                inside++;
            }
        }
        double pi = 4D * inside / size;
        return (int) pi;
    }

}