Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

What is a fast way to set all elements of an array to a given value without duplicating the (possibly large) array?

0
Posted

What is a fast way to set all elements of an array to a given value without duplicating the (possibly large) array?

0

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.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123