You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2015/12/21 03:20:50 UTC

[math] MATH-1305

Repository: commons-math
Updated Branches:
  refs/heads/master 813aa11d5 -> 88b29fa46


MATH-1305

Slight performance improvement of the "nextBytes" methods.


Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/88b29fa4
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/88b29fa4
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/88b29fa4

Branch: refs/heads/master
Commit: 88b29fa46bcf6f56df4213e1ba7810735a28d755
Parents: 813aa11
Author: Gilles <er...@apache.org>
Authored: Mon Dec 21 03:14:32 2015 +0100
Committer: Gilles <er...@apache.org>
Committed: Mon Dec 21 03:14:32 2015 +0100

----------------------------------------------------------------------
 src/changes/changes.xml                         |  5 +++
 .../math4/random/AbstractRandomGenerator.java   | 32 ++++++++++++--------
 .../math4/random/BitsStreamGenerator.java       | 32 ++++++++++++--------
 3 files changed, 45 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/88b29fa4/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 426d03c..c59d418 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -54,6 +54,11 @@ If the output is not quite correct, check for invisible trailing spaces!
     </release>
 
     <release version="4.0" date="XXXX-XX-XX" description="">
+      <action dev="erans" type="update" issue="MATH-1305" due-to="Rostislav Krasny">
+        "AbstractRandomGenerator" and "BitsStreamGenerator": Slight performance
+        improvement of the "nextBytes" method (particularly when the number of
+        requested bytes is a multiple of 4).
+      </action>
       <action dev="erans" type="fix" issue="MATH-1300" due-to="Rostislav Krasny">
         "AbstractRandomGenerator" and "BitsStreamGenerator": Remove a redundant call
         to the random data provider.

http://git-wip-us.apache.org/repos/asf/commons-math/blob/88b29fa4/src/main/java/org/apache/commons/math4/random/AbstractRandomGenerator.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/random/AbstractRandomGenerator.java b/src/main/java/org/apache/commons/math4/random/AbstractRandomGenerator.java
index c31dc68..2115dbd 100644
--- a/src/main/java/org/apache/commons/math4/random/AbstractRandomGenerator.java
+++ b/src/main/java/org/apache/commons/math4/random/AbstractRandomGenerator.java
@@ -107,21 +107,29 @@ public abstract class AbstractRandomGenerator implements RandomGenerator {
      */
     @Override
     public void nextBytes(byte[] bytes) {
-        int i = 0;
-        final int iEnd = bytes.length - 3;
-        while (i < iEnd) {
+        // Multiple 4 part of length (i.e. length with two least significant bits unset).
+        final int max = bytes.length & 0x7ffffffc;
+
+        int index = 0;
+        // Start filling in the byte array, 4 bytes at a time.
+        while (index < max) {
             final int random = nextInt();
-            bytes[i]     = (byte) (random & 0xff);
-            bytes[i + 1] = (byte) ((random >>  8) & 0xff);
-            bytes[i + 2] = (byte) ((random >> 16) & 0xff);
-            bytes[i + 3] = (byte) ((random >> 24) & 0xff);
-            i += 4;
+            bytes[index++] = (byte) random;
+            bytes[index++] = (byte) (random >>> 8);
+            bytes[index++] = (byte) (random >>> 16);
+            bytes[index++] = (byte) (random >>> 24);
         }
-        if (i < bytes.length) {
+
+        // Fill in the remaining bytes.
+        if (index < bytes.length) {
             int random = nextInt();
-            while (i < bytes.length) {
-                bytes[i++] = (byte) (random & 0xff);
-                random >>= 8;
+            while (true) {
+                bytes[index++] = (byte) random;
+                if (index < bytes.length) {
+                    random >>>= 8;
+                } else {
+                    break;
+                }
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/commons-math/blob/88b29fa4/src/main/java/org/apache/commons/math4/random/BitsStreamGenerator.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/random/BitsStreamGenerator.java b/src/main/java/org/apache/commons/math4/random/BitsStreamGenerator.java
index 7c89b60..4cf6823 100644
--- a/src/main/java/org/apache/commons/math4/random/BitsStreamGenerator.java
+++ b/src/main/java/org/apache/commons/math4/random/BitsStreamGenerator.java
@@ -72,21 +72,29 @@ public abstract class BitsStreamGenerator
     /** {@inheritDoc} */
     @Override
     public void nextBytes(byte[] bytes) {
-        int i = 0;
-        final int iEnd = bytes.length - 3;
-        while (i < iEnd) {
+        // Multiple 4 part of length (i.e. length with two least significant bits unset).
+        final int max = bytes.length & 0x7ffffffc;
+
+        int index = 0;
+        // Start filling in the byte array, 4 bytes at a time.
+        while (index < max) {
             final int random = next(32);
-            bytes[i]     = (byte) (random & 0xff);
-            bytes[i + 1] = (byte) ((random >>  8) & 0xff);
-            bytes[i + 2] = (byte) ((random >> 16) & 0xff);
-            bytes[i + 3] = (byte) ((random >> 24) & 0xff);
-            i += 4;
+            bytes[index++] = (byte) random;
+            bytes[index++] = (byte) (random >>> 8);
+            bytes[index++] = (byte) (random >>> 16);
+            bytes[index++] = (byte) (random >>> 24);
         }
-        if (i < bytes.length) {
+
+        // Fill in the remaing bytes.
+        if (index < bytes.length) {
             int random = next(32);
-            while (i < bytes.length) {
-                bytes[i++] = (byte) (random & 0xff);
-                random >>= 8;
+            while (true) {
+                bytes[index++] = (byte) random;
+                if (index < bytes.length) {
+                    random >>>= 8;
+                } else {
+                    break;
+                }
             }
         }
     }