What is a fast way to set all elements of an array to a given value without duplicating the (possibly large) array?
A. Using a loop that does it one by one is probably 20 to 40 times slower than good-old memset() in C. A fast way on many VM’s is to set the first byte of the array, then use System.arraycopy repeatedly to fill the next byte, the next two bytes, the next four bytes, the next eight bytes, etc. (Note these are not overlapping slices, so the issue raised in Q4.18 does not arise). public static void bytefill(byte[] array, byte value) { int len = array.