commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gilles (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (MATH-1306) Add public void nextBytes(byte[] bytes, int position, int length) method into the RandomGenerator interface
Date Sun, 27 Dec 2015 22:41:49 GMT

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

Gilles commented on MATH-1306:
------------------------------

bq. So why not to add the proposed method signature into CM as well?

I will since nobody was opposed.
Our local benchmark code did not show significant degradation (nor improvement) for the baseline
case due to the additional method indirection.
See MATH-1307 (which I've linked to this request).

Side note: For the case where only part of a big array is filled (i.e. your use-case), I have
either a ~8% improvement or ~40% degradation wrt the {{System.arraycopy}} version.  Since
you seem to like to get top performance, perhaps you could help figure out whether there are
flaws in the benchmarking code which I'm using.

> 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
>         Attachments: MersenneTwister1305.java, MersenneTwister1306.java
>
>
> 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)

Mime
View raw message