PoissonSamplerCachePerformance_jmhType_B1
public class PoissonSamplerCachePerformance
extends java.lang.Object
The benchmark is designed for a worse case scenario of Poisson means that are uniformly spread over a range and non-integer. A single sample is required per mean, E.g.
int min = 40; int max = 1000; int range = max - min; UniformRandomProvider rng = ...; // Compare ... for (int i = 0; i < 1000; i++) { PoissonSampler.of(rng, min + rng.nextDouble() * range).sample(); } // To ... PoissonSamplerCache cache = new PoissonSamplerCache(min, max); for (int i = 0; i < 1000; i++) { PoissonSamplerCache.createPoissonSampler(rng, min + rng.nextDouble() * range).sample(); }
The alternative scenario where the means are integer is not considered as this could be easily handled by creating an array to hold the PoissonSamplers for each mean. This does not require any specialised caching of state and is simple enough to perform for single threaded applications:
public class SimpleUnsafePoissonSamplerCache { int min = 50; int max = 100; PoissonSampler[] samplers = new PoissonSampler[max - min + 1]; public PoissonSampler createPoissonSampler(UniformRandomProvider rng, int mean) { if (mean < min || mean > max) { return PoissonSampler.of(rng, mean); } int index = mean - min; PoissonSampler sample = samplers[index]; if (sampler == null) { sampler = PoissonSampler.of(rng, mean); samplers[index] = sampler; } return sampler; } }
Note that in this example the UniformRandomProvider is also cached and so this is only
applicable to a single threaded application. Thread safety could be ensured using the
SharedStateSampler
functionality
of the cached sampler.
Re-written to use the PoissonSamplerCache would provide a new PoissonSampler per call in a thread-safe manner:
public class SimplePoissonSamplerCache { int min = 50; int max = 100; PoissonSamplerCache samplers = new PoissonSamplerCache(min, max); public PoissonSampler createPoissonSampler(UniformRandomProvider rng, int mean) { return samplers.createPoissonSampler(rng, mean); } }
Modifier and Type | Class | Description |
---|---|---|
static class |
PoissonSamplerCachePerformance.MeanRange |
The range of mean values for testing the cache.
|
static class |
PoissonSamplerCachePerformance.Sources |
The benchmark state (retrieve the various "RandomSource"s).
|
Constructor | Description |
---|---|
PoissonSamplerCachePerformance() |
Modifier and Type | Method | Description |
---|---|---|
void |
runPoissonSampler(PoissonSamplerCachePerformance.Sources sources,
PoissonSamplerCachePerformance.MeanRange range,
org.openjdk.jmh.infra.Blackhole bh) |
|
void |
runPoissonSamplerCache(PoissonSamplerCachePerformance.Sources sources,
PoissonSamplerCachePerformance.MeanRange range,
org.openjdk.jmh.infra.Blackhole bh) |
|
void |
runPoissonSamplerCacheWhenEmpty(PoissonSamplerCachePerformance.Sources sources,
PoissonSamplerCachePerformance.MeanRange range,
org.openjdk.jmh.infra.Blackhole bh) |
public void runPoissonSampler(PoissonSamplerCachePerformance.Sources sources, PoissonSamplerCachePerformance.MeanRange range, org.openjdk.jmh.infra.Blackhole bh)
sources
- Source of randomness.range
- The range.bh
- Data sink.public void runPoissonSamplerCacheWhenEmpty(PoissonSamplerCachePerformance.Sources sources, PoissonSamplerCachePerformance.MeanRange range, org.openjdk.jmh.infra.Blackhole bh)
sources
- Source of randomness.range
- The range.bh
- Data sink.public void runPoissonSamplerCache(PoissonSamplerCachePerformance.Sources sources, PoissonSamplerCachePerformance.MeanRange range, org.openjdk.jmh.infra.Blackhole bh)
sources
- Source of randomness.range
- The range.bh
- Data sink.Copyright © 2016–2019 The Apache Software Foundation. All rights reserved.