[
https://issues.apache.org/jira/browse/MATH-1306?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15071318#comment-15071318
]
Rostislav Krasny commented on MATH-1306:
----------------------------------------
{quote}Changing an interface might be a problem.{quote}
Yes. I just forgot that the {{RandomGenerator}} interface is an extraction of
the {{java.util.Random}} and so should be changed only if the
{{java.util.Random}} was.
{quote}We could perhaps add this in the base classes{quote}
Agreed.
{quote}Anyways, this issue should be raised on the "dev" ML.{quote}
Aren't discussions in the Jira tickets already forwarded into a mailing list
the MC developers a subscribed to? I just don't like scattering a discussion
over several places.
{quote}> to do that you need to use a temporary byte array and copy it by
yourself
>
Yes. Is it a problem?
Do you have a benchmark?{quote}
I thought this is obvious, but here is the benchmark. I used following code
where {{MersenneTwister1305}} and {{MersenneTwister1306}} are classes that
extend the standard MersenneTwister class of CM 3.5 and ovverride/add the
nextByte() methods as in the code committed in MATH-1305 and proposed by me
here in MATH-1306.
{code:java} long start;
long end;
final int iterations = 50000;
Runtime runtime = Runtime.getRuntime();
byte[] bigArray = new byte[128 * 1024 * 1024];
final int subArrayLength = 128 * 1024;
final int insertPosition = 8 * 1024 * 1024;
MersenneTwister1305 mt1305 = new MersenneTwister1305();
MersenneTwister1306 mt1306 = new MersenneTwister1306();
mt1305.setSeed(123456789123456789L);
mt1306.setSeed(123456789123456789L);
start = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) {
mt1306.nextBytes(bigArray, insertPosition,
subArrayLength);
}
end = System.currentTimeMillis();
System.out.println("Allocated memory: " +
(runtime.totalMemory() - runtime.freeMemory()));
System.out.printf("MATH-1306 MersenneTwister %d
iterations:\t%6d ms.\n", iterations, (end - start));
/*
* This code is memory hungry and GC may run long after this
section is finished.
* So put it last to not disturb the MATH-1306 testing
* */
start = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) {
byte[] subArray = new byte[subArrayLength];
mt1305.nextBytes(subArray);
System.arraycopy(subArray, 0, bigArray, insertPosition,
subArrayLength);
}
end = System.currentTimeMillis();
System.out.println("Allocated memory: " +
(runtime.totalMemory() - runtime.freeMemory()));
System.out.printf("MATH-1305 MersenneTwister %d
iterations:\t%6d ms.\n", iterations, (end - start));
{code}
It makes output like this:
{code}
Allocated memory: 135348016
MATH-1306 MersenneTwister 50000 iterations: 35687 ms.
Allocated memory: 172401784
MATH-1305 MersenneTwister 50000 iterations: 40328 ms.
{code}
{code}
Allocated memory: 135350224
MATH-1306 MersenneTwister 50000 iterations: 35859 ms.
Allocated memory: 172403992
MATH-1305 MersenneTwister 50000 iterations: 40406 ms.
{code}
As you can see the MATH-1306 version is better.
> Add public void nextBytes(byte[] bytes, int position, int length) method into
> the RandomGenerator interface
> -----------------------------------------------------------------------------------------------------------
>
> Key: MATH-1306
> URL: https://issues.apache.org/jira/browse/MATH-1306
> Project: Commons Math
> Issue Type: Improvement
> Affects Versions: 3.5
> Reporter: Rostislav Krasny
>
> I propose to add {{public void nextBytes(byte[] bytes, int position, int
> length)}} method into the {{RandomGenerator}} interface.
> Rationality: to improve performance and memory usage in cases when one needs
> to fill only a specified region of a byte array. Today to do that you need to
> use a temporary byte array and copy it by yourself into the specified region
> of the destination byte array.
> I propose the following code, based on the code of {{BitsStreamGenerator}}
> commited in MATH-1305
> {code:java}
> @Override
> public void nextBytes(byte[] bytes) {
> nextBytesFill(bytes, 0, bytes.length);
> }
> // TODO add this method into RandomGenerator interface
> //@Override
> public void nextBytes(byte[] bytes, int position, int length) {
> if (position < 0 || position > bytes.length - 1) {
> throw new
> OutOfRangeException(LocalizedFormats.OUT_OF_RANGE_SIMPLE, position, 0,
> bytes.length - 1);
> }
> if (length < 0 || length > bytes.length - position) {
> throw new
> OutOfRangeException(LocalizedFormats.OUT_OF_RANGE_SIMPLE, length, 0,
> bytes.length - position);
> }
> nextBytesFill(bytes, position, length);
> }
> private void nextBytesFill(byte[] bytes, int position, int length) {
> int index = position;
> // Position plus multiple 4 part of length (i.e. length with
> two least significant bits unset).
> final int indexLoopLimit = position + (length & 0x7ffffffc);
> // Start filling in the byte array, 4 bytes at a time.
> while (index < indexLoopLimit) {
> final int random = next(32);
> bytes[index++] = (byte) random;
> bytes[index++] = (byte) (random >>> 8);
> bytes[index++] = (byte) (random >>> 16);
> bytes[index++] = (byte) (random >>> 24);
> }
> final int indexLimit = position + length;
>
> // Fill in the remaining bytes.
> if (index < indexLimit) {
> int random = next(32);
> while (true) {
> bytes[index++] = (byte) random;
> if (index < indexLimit) {
> random >>>= 8;
> } else {
> break;
> }
> }
> }
> }
> {code}
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)