[
https://issues.apache.org/jira/browse/MATH-1307?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Otmar Ertl updated MATH-1307:
-----------------------------
Attachment: BaseRandomGeneratorFloatGenerationPerformanceTest.java
I have played around with the generation of floats and doubles from ints. On my
environment I made the interesting observation that following code which avoids
the floating point multiplication
{code}
public double nextDouble2() {
final long high = ((long) (nextInt() >>> 6)) << 26;
final int low = nextInt() >>> 6;
return Double.longBitsToDouble(0x3ff0000000000000L | high | low) - 1.;
}
{code}
is about 20% faster than
{code}
public double nextDouble() {
final long high = ((long) (nextInt() >>> 6)) << 26;
final int low = nextInt() >>> 6;
return (high | low) * 0x1.0p-52d;
}
{code}
Please see [^BaseRandomGeneratorFloatGenerationPerformanceTest.java] for which
I got following output:
{code}
nextDouble2: Time = 24.267s, Sum = 4.757900840407535E9
nextFloat: Time = 24.304s, Sum = 1.6777216E7
nextDouble: Time = 29.654s, Sum = 4.757900840407535E9
nextFloat2: Time = 24.276s, Sum = 1.6777216E7
{code}
> Create a base class for all RNGs
> --------------------------------
>
> Key: MATH-1307
> URL: https://issues.apache.org/jira/browse/MATH-1307
> Project: Commons Math
> Issue Type: Improvement
> Reporter: Gilles
> Assignee: Gilles
> Priority: Minor
> Labels: api, inheritance
> Fix For: 4.0
>
> Attachments: BaseRandomGenerator.java,
> BaseRandomGeneratorFloatGenerationPerformanceTest.java
>
>
> I proposed to create a base class which the existing abstract classes
> {{AbstractRandomGenerator}} and {{BitsStreamGenerator}} will extend.
> This would allow to define {{nextBytes(byte[])}} at the base class level.
> The code for that method is almost identical in the two hierarchies: they
> only differ in a call to either {{nextInt()}} or {{next(32)}} respectively;
> the latter is however the same as the former, in disguise, and is not subject
> to change given the type of return value.
> As a corollary, the new base class can be the unique place where to add
> utilities such as the one proposed in MATH-1306.
> *Update:* {{AbstractRandomGenerator}} and {{BitsStreamGenerator}} are both
> obsoleted by the class proposed in this report.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)