[ 
https://issues.apache.org/jira/browse/RNG-51?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16623697#comment-16623697
 ] 

Alex D Herbert commented on RNG-51:
-----------------------------------

I've created a PR with the modifications for review:

 [PR 10|https://github.com/apache/commons-rng/pull/10]

This includes changes to {{LargeMeanPoissonSampler}} and the new 
{{PoissonSamplerCache}} with tests.

JUnit tests have full coverage of the new code. I've considered what the PMD 
report was flagging (it doesn't like the {{LargeMeanPoissonSampler}} class) and 
have tried to add to {{LargeMeanPoissonSampler}} without impacting any existing 
functionality.

I've added a {{PoissonSamplerCachePerformance}} test to the JMH examples module.

In a best case scenario for the cache, i.e. using:
 - the fastest RNG
 - a uniform range over which the means are sampled
 - one sample per new instance of PoissonSampler

The benefit is noticeable:
||randomSourceName||range||Method||Score||Error||Relative||
|SPLIT_MIX_64|1|runPoissonSampler|39285.00|168.57|0.957|
|SPLIT_MIX_64|1|runPoissonSamplerCache|15210.17|101.50|0.370|
|SPLIT_MIX_64|1|runPoissonSamplerCacheWhenEmpty|41060.93|2959.63|1.000|
|SPLIT_MIX_64|4|runPoissonSampler|39793.03|133.41|0.892|
|SPLIT_MIX_64|4|runPoissonSamplerCache|15299.14|83.14|0.343|
|SPLIT_MIX_64|4|runPoissonSamplerCacheWhenEmpty|44629.41|6170.19|1.000|
|SPLIT_MIX_64|16|runPoissonSampler|41554.66|5311.96|1.000|
|SPLIT_MIX_64|16|runPoissonSamplerCache|16673.99|2810.09|0.401|
|SPLIT_MIX_64|16|runPoissonSamplerCacheWhenEmpty|39349.79|310.16|0.947|
|SPLIT_MIX_64|64|runPoissonSampler|46542.76|7700.87|1.000|
|SPLIT_MIX_64|64|runPoissonSamplerCache|17234.89|2994.09|0.370|
|SPLIT_MIX_64|64|runPoissonSamplerCacheWhenEmpty|43391.06|8280.36|0.932|
|SPLIT_MIX_64|256|runPoissonSampler|41466.23|6354.86|1.000|
|SPLIT_MIX_64|256|runPoissonSamplerCache|15958.17|2445.29|0.385|
|SPLIT_MIX_64|256|runPoissonSamplerCacheWhenEmpty|38321.65|169.43|0.924|
|SPLIT_MIX_64|1024|runPoissonSampler|48389.57|7976.99|1.000|
|SPLIT_MIX_64|1024|runPoissonSamplerCache|16979.87|2856.64|0.351|
|SPLIT_MIX_64|1024|runPoissonSamplerCacheWhenEmpty|38057.14|150.90|0.786|
|SPLIT_MIX_64|4096|runPoissonSampler|37847.80|318.28|0.976|
|SPLIT_MIX_64|4096|runPoissonSamplerCache|18817.36|3087.88|0.485|
|SPLIT_MIX_64|4096|runPoissonSamplerCacheWhenEmpty|38797.49|3142.02|1.000|

This was run using:
{noformat}
commons-rng/commons-rng-examples/examples-jmh > mvn -P benchmark test 
-Dbenchmark=org.apache.*PoissonSamplerCache*
{noformat}
I changed the POM to run the test 10 times just for these results (no permanent 
change to the POM).

>From these results:

- There is no noticeable slowdown when creating the {{PoissonSampler}} using 
the cache when the cache has no stored data.
- The cache is about 2 - 2.5x faster.

I will run a full test later with slower samplers and more tests (e.g. 30 
iterations).

I expect that when the sampler is slower then the performance improvement will 
be reduced. The speed of creating a sample using random numbers will be higher 
relative to the fixed cost of computing the algorithm constants.


> PoissonSampler single use speed improvements using a cache
> ----------------------------------------------------------
>
>                 Key: RNG-51
>                 URL: https://issues.apache.org/jira/browse/RNG-51
>             Project: Commons RNG
>          Issue Type: Improvement
>    Affects Versions: 1.1
>            Reporter: Alex D Herbert
>            Priority: Minor
>              Labels: performance
>
> This improvement is a follow on from RNG-50.
> That improved the PoissonSampler by splitting it into two samplers for the 
> different algorithms (SmallMeanPoissonSampler and LargeMeanPoissonSampler) 
> and moving initialisation computations from the sample method into the 
> constructor. This improved the speed of repeat use of the same sampler by up 
> to 3-fold. It does not effect single-use of a sampler.
> In the case of the LargeMeanPoissonSampler the initialisation 'state' 
> computed within the constructor involves 2 Math.log, 2 Math.exp, 2 Math.sqrt 
> and 1 logFactorial computations. If the sampler is used to generate only 1 
> sample then the cost of computing the state is a significant portion of the 
> sample time.
> However the state is not unique to the mean of the Poisson distribution but 
> the {{(int)Math.floor(mean)}}. This means the initialisation state can be 
> shared and reused by PoissonSamplers with different means.
> This was demonstrated using a JMH benchmark speed test comparing the standard 
> PoissonSampler verses a PoissonSamplerCache that can store the initialisation 
> state of the PoissonSampler.
> For single-use Poisson samples with a mean within a known range the cache can 
> provide 1.3 to 2x speed-up as initialisation state can be reused.
> The original benchmark was performed using Version 1.0. The Version 1.1. 
> samplers use a new faster Gaussian sampler internally. So the benchmark 
> should be repeated for Version 1.1.
> The test project containing the V1.0 JMH benchmark is here: 
> [poisson-sampler-test-code|https://github.com/aherbert/poisson-sampler-test-code]
> I propose to update this project to reference V1.1 of Commons RNG and repeat 
> the test of the PoissonSamplerCache. Results will be posted when available. 



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to