[ 
https://issues.apache.org/jira/browse/MATH-1305?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Rostislav Krasny updated MATH-1305:
-----------------------------------
    Description: 
I propose to use this code in {{BitsStreamGenerator}}
{code:java}
        @Override
        public void nextBytes(byte[] bytes) {
                int index = 0;

                // multiple 4 part of length, i.e. length with two least 
significant bits unset
                int max = bytes.length & 0x7ffffffc;

                // start filling the byte array with tetrads of bytes
                while (index < max) {
                        int random = next(32);
                        bytes[index++] = (byte) random;
                        bytes[index++] = (byte) (random >>> 8);
                        bytes[index++] = (byte) (random >>> 16);
                        bytes[index++] = (byte) (random >>> 24);
                }

                // fill the remains bytes
                if (index < bytes.length) {
                        int random = next(32);
                        while (true) {
                                bytes[index++] = (byte) random;
                                if (index < bytes.length) {
                                        random >>>= 8;
                                } else {
                                        break;
                                }
                        }
                }
        }
{code}
I also propose to use the same code but with {{nextInt()}} calls instead of 
{{next(32)}} in the {{AbstractRandomGenerator}}. This implementation improves 
performance and fixes inconsistency bugs in those two classes discussed in the 
MATH-1300. It is also quite simple and well commented.

  was:
I propose to use this code in BitsStreamGenerator
{code:java}
        @Override
        public void nextBytes(byte[] bytes) {
                int index = 0;

                // multiple 4 part of length, i.e. length with two least 
significant bits unset
                int max = bytes.length & 0x7ffffffc;

                // start filling the byte array with tetrads of bytes
                while (index < max) {
                        int random = next(32);
                        bytes[index++] = (byte) random;
                        bytes[index++] = (byte) (random >>> 8);
                        bytes[index++] = (byte) (random >>> 16);
                        bytes[index++] = (byte) (random >>> 24);
                }

                // fill the remains bytes
                if (index < bytes.length) {
                        int random = next(32);
                        while (true) {
                                bytes[index++] = (byte) random;
                                if (index < bytes.length) {
                                        random >>>= 8;
                                } else {
                                        break;
                                }
                        }
                }
        }
{code}
I also propose to use the same code but with nextInt() calls instead of 
next(32) in the AbstractRandomGenerator. This implementation improves 
performance and fixes inconsistency bugs in those two classes discussed in the 
MATH-1300. It is also quite simple and well commented.


> Improve performance of nextBytes() method of BitsStreamGenerator and 
> AbstractRandomGenerator 
> ---------------------------------------------------------------------------------------------
>
>                 Key: MATH-1305
>                 URL: https://issues.apache.org/jira/browse/MATH-1305
>             Project: Commons Math
>          Issue Type: Improvement
>    Affects Versions: 4.0, 3.5, 3.6
>            Reporter: Rostislav Krasny
>
> I propose to use this code in {{BitsStreamGenerator}}
> {code:java}
>       @Override
>       public void nextBytes(byte[] bytes) {
>               int index = 0;
>               // multiple 4 part of length, i.e. length with two least 
> significant bits unset
>               int max = bytes.length & 0x7ffffffc;
>               // start filling the byte array with tetrads of bytes
>               while (index < max) {
>                       int random = next(32);
>                       bytes[index++] = (byte) random;
>                       bytes[index++] = (byte) (random >>> 8);
>                       bytes[index++] = (byte) (random >>> 16);
>                       bytes[index++] = (byte) (random >>> 24);
>               }
>               // fill the remains bytes
>               if (index < bytes.length) {
>                       int random = next(32);
>                       while (true) {
>                               bytes[index++] = (byte) random;
>                               if (index < bytes.length) {
>                                       random >>>= 8;
>                               } else {
>                                       break;
>                               }
>                       }
>               }
>       }
> {code}
> I also propose to use the same code but with {{nextInt()}} calls instead of 
> {{next(32)}} in the {{AbstractRandomGenerator}}. This implementation improves 
> performance and fixes inconsistency bugs in those two classes discussed in 
> the MATH-1300. It is also quite simple and well commented.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to