Skip to main content

Question about the performance of Arrays.fill(boolean[] a, boolean val)

1 reply [Last post]
Joined: 2007-10-09

i found it was implemented as the follows code shows:
public static void fill(boolean[] a, boolean val)
?? ??? ?int i = 0, ??? = a.length; i < ???; i++?
a?i? = val?;

I wonder if this code is efficient enough, since i may use Arrays.fill(test, true), while the test array is quite large, for example, i defined test as new boolean[100000000], so it must iterate 100000000 times to initialize this test array, so i doubt this code with be efficient enough. Is there some underlying mechanism to ensure its efficiency? Then what is it? the compiler optimization technology? or sth else
I know C++ has a similar function named memset(void *s, int c, size_t n), its function is to make a block of memory to specific value, then what is the difference between memset and Arrays.fill(), may anyone give your view about this problem? thx

Message was edited by: wordperfect

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2004-01-07

At least the server-compiler will unroll this loop.

I also played arround with some manual unrolling some time ago, and some other low-level optimizations coded directly by hand but this only confused hotspot - and made the code actually run slower! So don't worry ... hotspot (the jit-compiler used/included in Sun-Java) will do absolute fine in these situations ^^

lg Clemens