0
001
I want to initialize a large array with 0's... do I have to use a for-loop
or is there some trick to accomplish this?
or is there some trick to accomplish this?
001 said:I want to initialize a large array with 0's... do I have to use a for-loop
or is there some trick to accomplish this?
001 said:I want to initialize a large array with 0's... do I have to use a for-loop
or is there some trick to accomplish this?
new int[n] is an all zero int array of size n. If you have an existing
array and need to reinitialize it to zero, use Arrays.fill.
Out of five runs (Java 1.6 on WinXP on an AMP ..
new int[n] is an all zero int array of size n. If you have an existing
array and need to reinitialize it to zero, use Arrays.fill.
I 'object' to such simplistic answers to what is
effectively a complex question. My first question
for the OP would be, what do you mean by 'quick'?
There are two potential meanings, AFAIU.
1) quick to code.
2) quick to run.
Why did you fill the int array with 1's in the for loop test
and 0's in the Arrays.fill test?
Oh. (Are JIT compilers that smart?)
Or--at least--initialize a different array in
each test.
Andrew Thompson said:001 said:I want to initialize a large array with 0's... do I have to use a
for-loop
or is there some trick to accomplish this?
new int[n] is an all zero int array of size n. If you have an existing
array and need to reinitialize it to zero, use Arrays.fill.
I 'object' to such simplistic answers to what is
effectively a complex question. My first question
for the OP would be, what do you mean by 'quick'?
There are two potential meanings, AFAIU.
1) quick to code.
2) quick to run.
The first is inconsequential, the only point
to writing a shorter method is for the purposes
of code clarity (which has little to do with
'quick').
And for the second, I am not convinced that
Arrays.fill() takes less CPU cycles.
Here is the test code/results I am seeing..
<sscce>
import java.util.Arrays;
class InitialiseToZero {
public static void main(String[] args) {
int length = 10000001;
int[] intArray = new int[length];
long startTimeOfLoop = System.currentTimeMillis();
for (int ii=0; ii<length; ii++) {
intArray[ii] = 1;
}
long endTimeOfLoop = System.currentTimeMillis();
long startTimeOfFill = System.currentTimeMillis();
Arrays.fill( intArray, 0 );
long endTimeOfFill = System.currentTimeMillis();
System.out.println( "Time to loop " +
(endTimeOfLoop - startTimeOfLoop) );
System.out.println( "Time to fill " +
(endTimeOfFill - startTimeOfFill) );
}
}
</sscce>
Andrew Thompson said:On Apr 1, 4:05 am, "George Cherry" <
..
"Because I'm evil".
LOL, sometimes I crack me up. ;-)
Why not? I was (ever so slightly) worried that
the JIT compiler would notice the inefficiency of
doing something that had already been done, and
ignore the second call.
(muses) maybe I should
have put all 'random' values..
Well.. altered code, different test results,
would be more than welcome on this thread..
How about it?
"Let the (altered) code (and test results) do
the talkin'"
Andrew Thompson wrote: .... ...
OK, here's a new version. It does a new call, with a new array
instance, for each test. I tested three approaches:
Andrew Thompson said:001 said:I want to initialize a large array with 0's... do I have to use a
for-loop
or is there some trick to accomplish this?
new int[n] is an all zero int array of size n. If you have an existing
array and need to reinitialize it to zero, use Arrays.fill.
I 'object' to such simplistic answers to what is
effectively a complex question. My first question
for the OP would be, what do you mean by 'quick'?
There are two potential meanings, AFAIU.
1) quick to code.
2) quick to run.
The first is inconsequential, the only point
to writing a shorter method is for the purposes
of code clarity (which has little to do with
'quick').
And for the second, I am not convinced that
Arrays.fill() takes less CPU cycles.
Here is the test code/results I am seeing..
<sscce>
import java.util.Arrays;
class InitialiseToZero {
public static void main(String[] args) {
int length = 10000001;
int[] intArray = new int[length];
long startTimeOfLoop = System.currentTimeMillis();
for (int ii=0; ii<length; ii++) {
intArray[ii] = 1;
}
long endTimeOfLoop = System.currentTimeMillis();
long startTimeOfFill = System.currentTimeMillis();
Arrays.fill( intArray, 0 );
long endTimeOfFill = System.currentTimeMillis();
System.out.println( "Time to loop " +
(endTimeOfLoop - startTimeOfLoop) );
System.out.println( "Time to fill " +
(endTimeOfFill - startTimeOfFill) );
}
}
</sscce>
Time to loop 94
Time to fill 140
Press any key to continue . . .
Time to loop 140
Time to fill 110
Press any key to continue . . .
Time to loop 125
Time to fill 140
Press any key to continue . . .
Time to loop 125
Time to fill 109
Press any key to continue . . .
Time to loop 141
Time to fill 141
Press any key to continue . . .
Out of five runs (Java 1.6 on WinXP on an AMP
XP 1800 CPU), the loop was faster on two
occasions, slower on two, and equal on the
last.
'Six of one - half a dozen of the other.'
as I see it.
Andrew T.
George said:Oh. (Are JIT compilers that smart?)
Patricia said:"trustTheJVM" is the clear winner, and should be used unless there is a
need to reinitialize the array after it has been modified.
For small arrays, if reinitialization is needed, "loop" wins over "fill"
by a reasonable margin.
For large arrays, "loop" and "fill" have similar performance.
Arne said:No surprise since Arrays.fill use the same loop but
has a couple of extra calls.
001 said:Yes, you're right...
..I really mean quick to run as that's the most important
thing in life isn't it?
Mark said:Just because the code looks the same does not mean it will have the same
performance when one implementation is part of the standard library. The
JVM might replace library implementations with an accelerated form
(intrinsic).
Thus Patricia is correct to test the actual performance of
these methods, and that performance might change in a future JVM.
001 said:I really mean quick to run as that's the most important thing in life
isn't it?
Thanks for all your responses guys! The thing is I didn't have to
reinitialize it... I only wanted to initialize it once... (but I thought
relying on the zero's when an array is declared would result in a
compile-time error and I am therefore really sorry to find out only now
that that wasn't the case! (as it is with normal variables)).
Oliver said:If that were the case, you probably wouldn't be programming in
Java. Instead, you'd be designing dedicated hardware to perform the
task.
Fields are automatically initialized to default values (0 for
integers, false for booleans, etc.) Local variables are not
automatically initialized at all (not even with random values).
Perhaps you're thinking of C?
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.