reinitialize byte array to zero

J

johndesp

I have a byte array that I initialize by getting bytes from another
object. After my first usage I need to clean out the byte array..how
can i do this?

byte mollyarray [] = someotherobject.getbytes();

//after I am done....need to clean out mollyarray



Can you please tell me the best way to do this?

thanks
 
S

Steve Horsley

johndesp said:
I have a byte array that I initialize by getting bytes from another
object. After my first usage I need to clean out the byte array..how
can i do this?

byte mollyarray [] = someotherobject.getbytes();

//after I am done....need to clean out mollyarray



Can you please tell me the best way to do this?

thanks

for(int i = 0 ; i < mollyarray.length ; i++) {
mollyarray = 0;
}


It may actually be quicker to allocate a new array, you would need to
test this if performance is important:

mollyarray = new byte[mollyarray.length];


Steve
 
M

Marc Dzaebel

johndesp said:
byte mollyarray [] = someotherobject.getbytes();
//after I am done....need to clean out mollyarray

java.util.Arrays.fill(mollyarray,0);

you may also specify fromIndex and toIndex.
 
T

Tony Morris

Arrays.fill(mollyarray, (byte)0);

Alternatively, and less recommended:

for(int i = 0; i < mollarray.length; i++)
{
mollyarray = 0;
}

The Arrays method is likely to be implemented as a native method and
therefore, will perform better than the less recommended solution.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
T

Tony Morris

Neither of those is the most efficient solution.
@see java.util.Arrays

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


Steve Horsley said:
johndesp said:
I have a byte array that I initialize by getting bytes from another
object. After my first usage I need to clean out the byte array..how
can i do this?

byte mollyarray [] = someotherobject.getbytes();

//after I am done....need to clean out mollyarray



Can you please tell me the best way to do this?

thanks

for(int i = 0 ; i < mollyarray.length ; i++) {
mollyarray = 0;
}


It may actually be quicker to allocate a new array, you would need to
test this if performance is important:

mollyarray = new byte[mollyarray.length];


Steve
 
T

Tony Morris

This will not compile.
There is no such method as Arrays.fill(byte[], int)

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


Marc Dzaebel said:
johndesp said:
byte mollyarray [] = someotherobject.getbytes();
//after I am done....need to clean out mollyarray

java.util.Arrays.fill(mollyarray,0);

you may also specify fromIndex and toIndex.
 
J

Jon A. Cruz

johndesp said:
I have a byte array that I initialize by getting bytes from another
object. After my first usage I need to clean out the byte array..how
can i do this?

byte mollyarray [] = someotherobject.getbytes();

//after I am done....need to clean out mollyarray

Depends on what you mean by "clean out".

Given that it is set by getting a byte array from some other place to
begin with, this would seem like the most appropriate approach to me:

mollyarray = new byte[0];


It has now been replaced by an array that is empty, and has a size of zero.

Oh, also... your declaration would be more "java-like" if you moved the
square braces:


byte[] mollyarray = someotherobject.getbytes();
 
J

Jon A. Cruz

Tony said:
Arrays.fill(mollyarray, (byte)0);
....

However... there is one 'gotcha' that should be addressed.

Since the OP is getting an array handed to him, instead of filling an
array that he created, it's possible he'd be pulling the rug out from
somewhere else.

That is, he should just be sure who 'owns' that array and that there are
no other references to it that might be used for conflicting access.

(Probably not in this case, but always good to be aware of)
 
T

Tony Morris

indeed, hence, the idea of passing around mutable types (in this case, an
array) must be handled with care, if at all.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
A

Andrew Hobbs

Tony Morris said:
Arrays.fill(mollyarray, (byte)0);

Alternatively, and less recommended:

for(int i = 0; i < mollarray.length; i++)
{
mollyarray = 0;
}

The Arrays method is likely to be implemented as a native method and
therefore, will perform better than the less recommended solution.


Actually the for() loop is faster. On my machine running 1.4.1 the second
method consistently runs about 20% faster than the Arrays.fill() method.

That doesn't mean that I would use it unless speed was super important at
that point. The fill() method is much neater and its function more obvious.

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
(e-mail address removed)

*********************************************************

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


johndesp said:
I have a byte array that I initialize by getting bytes from another
object. After my first usage I need to clean out the byte array..how
can i do this?

byte mollyarray [] = someotherobject.getbytes();

//after I am done....need to clean out mollyarray



Can you please tell me the best way to do this?

thanks
 
N

nos

don't forget the possibility of reading from a disk file
to fill the array

Jon A. Cruz said:
johndesp said:
I have a byte array that I initialize by getting bytes from another
object. After my first usage I need to clean out the byte array..how
can i do this?

byte mollyarray [] = someotherobject.getbytes();

//after I am done....need to clean out mollyarray

Depends on what you mean by "clean out".

Given that it is set by getting a byte array from some other place to
begin with, this would seem like the most appropriate approach to me:

mollyarray = new byte[0];


It has now been replaced by an array that is empty, and has a size of zero.

Oh, also... your declaration would be more "java-like" if you moved the
square braces:


byte[] mollyarray = someotherobject.getbytes();
 
T

Tony Morris

According to my benchmarks, Arrays.fill is approximately 10% faster on Sun
1.4.2_03, Windows 2003.
Perhaps your test case code may have contained flaws ? or the VM has been
improved ?

/*
Some example test runs

C:\>java Test 100000
843
953

C:\>java Test 1000000
8452
9436
*/
import java.util.Arrays;

public class Test
{
public static void main(String[] args)
{
byte[] x = new byte[1024];
byte b = 0;
long times = Long.parseLong(args[0]);

long time1 = System.currentTimeMillis();

for(int i = 0; i < times; i++)
{
Arrays.fill(x, b);
}

long time2 = System.currentTimeMillis();

for(int i = 0; i < times; i++)
{
for(int j = 0; j < x.length; j++)
{
x[j] = b;
}
}

long time3 = System.currentTimeMillis();

System.out.println(time2 - time1);
System.out.println(time3 - time2);

}
}



--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


Andrew Hobbs said:
Tony Morris said:
Arrays.fill(mollyarray, (byte)0);

Alternatively, and less recommended:

for(int i = 0; i < mollarray.length; i++)
{
mollyarray = 0;
}

The Arrays method is likely to be implemented as a native method and
therefore, will perform better than the less recommended solution.


Actually the for() loop is faster. On my machine running 1.4.1 the second
method consistently runs about 20% faster than the Arrays.fill() method.

That doesn't mean that I would use it unless speed was super important at
that point. The fill() method is much neater and its function more obvious.

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
(e-mail address removed)

*********************************************************

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


johndesp said:
I have a byte array that I initialize by getting bytes from another
object. After my first usage I need to clean out the byte array..how
can i do this?

byte mollyarray [] = someotherobject.getbytes();

//after I am done....need to clean out mollyarray



Can you please tell me the best way to do this?

thanks
 
T

Tony Morris

Surprisingly, the IBM 1.3.1 and 1.4.1 VM on Windows 2003 executes
Arrays.fill 5-10 times faster.
I don't have access to Sun 1.4.1, I'd be surprised to see the result.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


Tony Morris said:
According to my benchmarks, Arrays.fill is approximately 10% faster on Sun
1.4.2_03, Windows 2003.
Perhaps your test case code may have contained flaws ? or the VM has been
improved ?

/*
Some example test runs

C:\>java Test 100000
843
953

C:\>java Test 1000000
8452
9436
*/
import java.util.Arrays;

public class Test
{
public static void main(String[] args)
{
byte[] x = new byte[1024];
byte b = 0;
long times = Long.parseLong(args[0]);

long time1 = System.currentTimeMillis();

for(int i = 0; i < times; i++)
{
Arrays.fill(x, b);
}

long time2 = System.currentTimeMillis();

for(int i = 0; i < times; i++)
{
for(int j = 0; j < x.length; j++)
{
x[j] = b;
}
}

long time3 = System.currentTimeMillis();

System.out.println(time2 - time1);
System.out.println(time3 - time2);

}
}



--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


Andrew Hobbs said:
Tony Morris said:
Arrays.fill(mollyarray, (byte)0);

Alternatively, and less recommended:

for(int i = 0; i < mollarray.length; i++)
{
mollyarray = 0;
}

The Arrays method is likely to be implemented as a native method and
therefore, will perform better than the less recommended solution.


Actually the for() loop is faster. On my machine running 1.4.1 the second
method consistently runs about 20% faster than the Arrays.fill() method.

That doesn't mean that I would use it unless speed was super important at
that point. The fill() method is much neater and its function more obvious.

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
(e-mail address removed)

*********************************************************

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


I have a byte array that I initialize by getting bytes from another
object. After my first usage I need to clean out the byte array..how
can i do this?

byte mollyarray [] = someotherobject.getbytes();

//after I am done....need to clean out mollyarray



Can you please tell me the best way to do this?

thanks

 
A

Andrew Hobbs

Hi,

I copy/pasted your code and got the results below. I had to make a couple
of slight changes to allow it to run within the environment I use and to get
several results to check for consistency, but the results seem to be pretty
consistent. One thing that I did notice is that the time difference is a
bit smaller if I am filling many small arrays rather than a few large
arrays, but the for loop still comes out faster.

import java.util.Arrays;

public class Test
{
public static void main(String[] args)
{

for(int k = 0; k < 5; k++) {
byte[] x = new byte[1024];
byte b = 0;
long times = 1000000;

long time1 = System.currentTimeMillis();

for(int i = 0; i < times; i++)
{
Arrays.fill(x, b);
}

long time2 = System.currentTimeMillis();

for(int i = 0; i < times; i++)
{
for(int j = 0; j < x.length; j++)
{
x[j] = b;
}
}

long time3 = System.currentTimeMillis();

System.out.println("Fill time = " + (time2 - time1));
System.out.println("For loop time = " + (time3 - time2));

}
}
}

Fill time = 3625

For loop time = 3375

Fill time = 3672

For loop time = 3375

Fill time = 3640

For loop time = 3391

Fill time = 3625

For loop time = 3391

Fill time = 3672

For loop time = 3468

Cheers

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
(e-mail address removed)

*********************************************************
 
T

Tony Morris

You have prompted me to run the test on various VMs.
Following are conclusions drawn from multiple test runs.
All test runs and compilations are performed on the Windows 2003 platform.
Machine Specification: dual Intel 1 GHz CPUs, 2 GB DDR RAM.
Ratios given are the result on the average of a number of test runs.
Array size was kept constant at size 1024 - this value likely affects the
outcome of the test results.

Running on Sun JVM 1.4.0
Compiled with Sun SDK 1.4.2_02
Conclusion: Arrays.fill to "for loop" performance ratio: 750:938

Compiled with Sun SDK 1.4.0
Conclusion: Arrays.fill to "for loop" performance ratio: 838:573

Running on Sun JVM 1.4.1_03
Compiled with Sun SDK 1.4.2_02
Conclusion: Arrays.fill to "for loop" performance ratio: 1024:833

Compiled with Sun SDK 1.4.1_03
Conclusion: Arrays.fill to "for loop" performance ratio: 1094:1140

Running on Sun JVM 1.4.2_02
Compiled with Sun SDK 1.4.2_02
Conclusion: Arrays.fill to "for loop" performance ratio: 828:953

Running on Sun JVM 1.5.0 beta
Compiled with Sun SDK 1.4.2_02
Conclusion: Arrays.fill to "for loop" performance ratio: 1042:740

Compiled with Sun SDK 1.5.0 beta
Conclusion: Arrays.fill to "for loop" performance ratio: 1047:735

Running on IBM JVM 1.3.1
Compiled with Sun SDK 1.4.2_02
Conclusion: Arrays.fill to "for loop" performance ratio: 359:219

Compiled with IBM SDK 1.3.1
Conclusion: Arrays.fill to "for loop" performance ratio: 359:219

Running on IBM JVM 1.4.1
Compiled with Sun SDK 1.4.2_02
Conclusion: Arrays.fill to "for loop" performance ratio: 359:210

Compiled with IBM SDK 1.4.1
Conclusion: Arrays.fill to "for loop" performance ratio: 360:203

Overall conclusion
The test runs indicate that there is bytecode optimisation performed by
different compilers.
Interestingly, the Arrays.fill performance seemed to improve with later
versions of the JVM with the exception of Sun 1.4.1, but fell when using
1.5.0, however, it must be noted that this JVM is in beta state.
It may even be speculated that any optimisation that is occurring, is yet to
be implemented in the 1.5.0 JVM. The authors of the Sun 1.5.0 JVM will be
consulted regarding these results.

From a maintenance perspective (as opposed to performance), duplicating core
API functionality is generally poor practice, and therefore, the use of
Arrays.fill is recommended.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


Andrew Hobbs said:
Hi,

I copy/pasted your code and got the results below. I had to make a couple
of slight changes to allow it to run within the environment I use and to get
several results to check for consistency, but the results seem to be pretty
consistent. One thing that I did notice is that the time difference is a
bit smaller if I am filling many small arrays rather than a few large
arrays, but the for loop still comes out faster.

import java.util.Arrays;

public class Test
{
public static void main(String[] args)
{

for(int k = 0; k < 5; k++) {
byte[] x = new byte[1024];
byte b = 0;
long times = 1000000;

long time1 = System.currentTimeMillis();

for(int i = 0; i < times; i++)
{
Arrays.fill(x, b);
}

long time2 = System.currentTimeMillis();

for(int i = 0; i < times; i++)
{
for(int j = 0; j < x.length; j++)
{
x[j] = b;
}
}

long time3 = System.currentTimeMillis();

System.out.println("Fill time = " + (time2 - time1));
System.out.println("For loop time = " + (time3 - time2));

}
}
}

Fill time = 3625

For loop time = 3375

Fill time = 3672

For loop time = 3375

Fill time = 3640

For loop time = 3391

Fill time = 3625

For loop time = 3391

Fill time = 3672

For loop time = 3468

Cheers

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
(e-mail address removed)

*********************************************************




Tony Morris said:
Surprisingly, the IBM 1.3.1 and 1.4.1 VM on Windows 2003 executes
Arrays.fill 5-10 times faster.
I don't have access to Sun 1.4.1, I'd be surprised to see the result.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
A

Andrew Hobbs

Andrew Hobbs said:
Hi,


Fill time = 3625

For loop time = 3375

Fill time = 3672

For loop time = 3375

Fill time = 3640

For loop time = 3391

Fill time = 3625

For loop time = 3391

Fill time = 3672

For loop time = 3468


Note that slight extra time involved in calling the 'fill' method is because
the fill(byte[], byte) method calls a second method fill(byte[], firstIndex,
lastIndex, byte) which is used to fill an array only partially. The extra
time represents the overhead of calling that extra method (Note the second
method also validates the indices but this validation method call does not
add substantially to the time). (Note that the method in Arrays.fill()
simply uses a for loop anyway. (At least in Sun 1.4.1).

If one collapses the two methods in the Arrays.java class (but leaving in a
bogus call to the validation method) the time difference is eliminated.

Cheers

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
(e-mail address removed)

*********************************************************
Cheers

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
(e-mail address removed)

*********************************************************




Tony Morris said:
Surprisingly, the IBM 1.3.1 and 1.4.1 VM on Windows 2003 executes
Arrays.fill 5-10 times faster.
I don't have access to Sun 1.4.1, I'd be surprised to see the result.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
A

Andrew Hobbs

Overall conclusion
The test runs indicate that there is bytecode optimisation performed by
different compilers.
Interestingly, the Arrays.fill performance seemed to improve with later
versions of the JVM with the exception of Sun 1.4.1, but fell when using
1.5.0, however, it must be noted that this JVM is in beta state.
It may even be speculated that any optimisation that is occurring, is yet to
be implemented in the 1.5.0 JVM. The authors of the Sun 1.5.0 JVM will be
consulted regarding these results.

Interesting to see the variation.
From a maintenance perspective (as opposed to performance), duplicating core
API functionality is generally poor practice, and therefore, the use of
Arrays.fill is recommended.

I agree. Only when you are trying to squeeze every last drop of speed out
for a particular application and one is in the end stages of optimization
would I worry about that sort of difference.

Cheers

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
(e-mail address removed)

*********************************************************


--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


Andrew Hobbs said:
Hi,

I copy/pasted your code and got the results below. I had to make a couple
of slight changes to allow it to run within the environment I use and to get
several results to check for consistency, but the results seem to be pretty
consistent. One thing that I did notice is that the time difference is a
bit smaller if I am filling many small arrays rather than a few large
arrays, but the for loop still comes out faster.

import java.util.Arrays;

public class Test
{
public static void main(String[] args)
{

for(int k = 0; k < 5; k++) {
byte[] x = new byte[1024];
byte b = 0;
long times = 1000000;

long time1 = System.currentTimeMillis();

for(int i = 0; i < times; i++)
{
Arrays.fill(x, b);
}

long time2 = System.currentTimeMillis();

for(int i = 0; i < times; i++)
{
for(int j = 0; j < x.length; j++)
{
x[j] = b;
}
}

long time3 = System.currentTimeMillis();

System.out.println("Fill time = " + (time2 - time1));
System.out.println("For loop time = " + (time3 - time2));

}
}
}

Fill time = 3625

For loop time = 3375

Fill time = 3672

For loop time = 3375

Fill time = 3640

For loop time = 3391

Fill time = 3625

For loop time = 3391

Fill time = 3672

For loop time = 3468

Cheers

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
(e-mail address removed)

*********************************************************




Tony Morris said:
Surprisingly, the IBM 1.3.1 and 1.4.1 VM on Windows 2003 executes
Arrays.fill 5-10 times faster.
I don't have access to Sun 1.4.1, I'd be surprised to see the result.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
G

Guest

Arrays.fill(mollyarray, (byte)0);

Alternatively, and less recommended:

for(int i = 0; i < mollarray.length; i++) {
mollyarray = 0;
}
}
The Arrays method is likely to be implemented as a native method and
therefore, will perform better than the less recommended solution.


For a faster for loop, try this:

synchronized ( mollyarray)
{
for ( int i = mollyarray.length - 1; i >= 0; i--)
{
maollyarray = (byte) 0;
}
}

This way the length of the array is only calculated once. It is also a
single JASM instruction to compare a value to zero.

-- La'ie
 
A

Andrew Hobbs

La?ie Techie said:
Arrays.fill(mollyarray, (byte)0);

Alternatively, and less recommended:

for(int i = 0; i < mollarray.length; i++) {
mollyarray = 0;
}
}
The Arrays method is likely to be implemented as a native method and
therefore, will perform better than the less recommended solution.


For a faster for loop, try this:

synchronized ( mollyarray)
{
for ( int i = mollyarray.length - 1; i >= 0; i--)
{
maollyarray = (byte) 0;
}
}

This way the length of the array is only calculated once. It is also a
single JASM instruction to compare a value to zero.


Have you tried this? I just did this comparing count up vs countdown and it
made absolutely no difference.

Andrew

********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
(e-mail address removed)

*********************************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,006
Latest member
LauraSkx64

Latest Threads

Top