0

In a performance-critical part of my code, I need to clear an int array buffer by setting it back to all 0s.

Should I do buffer = new int[size] or Arrays.fill(buffer, 0)? The first seems to be faster in my tests, but maybe it will slow down eventually because of garbage collection. I don't have confidence in my own tests (because of stuff like compiler optimization), so I am asking it here.

If it matters, buffer will be size of about 300, and I need to clear buffer when it fills up, so after 300 iterations of my main loop.

I read More efficient to create new array or reset array but it doesn't specifically say for larger arrays. Also it is for Objects, not ints, which I think could matter.

2
  • 1
    Why do you need to clear this buffer? Presumably you're already keeping track of its index range; just ignore everything outside it. (And the answer will almost certainly be identical regardless of whether the array contains references or ints.) Commented Feb 23, 2021 at 0:39
  • hmm I will look into that, thank you. But for future reference (if this wasn't about buffers but just in general), which one is faster? Commented Feb 23, 2021 at 0:56

1 Answer 1

2

Is it faster to create new array or set all elements of current array to 0.

There is no simple answer. The JVM can allocate a default initialized array faster that fill(array, 0) can fill an array of the same size. But the flipside is that there are GC-related overheads that are difficult to quantify:

  • The GC costs are typically proportional to amount of reachable data. For non-reachable objects, the cost is essentially the cost of zeroing memory.

  • The GC costs / efficiency will depend on the heap size, and on how full it is.

  • The GC overheads also depend on the lifetime of the objects. For example a long-lived object will typically be tenured to the "old" generation and GC'd less often. But the flipside is that write barriers may make array writes slower.

  • Different GC's perform differently.

  • Different Java JIT compilers, etc perform differently.

  • And so on.

The bottom line is that it is not possible to give a clear answer without knowing ... more information than you can provide to create a valid model of the behavior.

Likewise, artificial benchmarks are liable to involve making explicit or implicit choices about various of the above (overt and hidden) variables. The result is liable to be that the benchmark results don't reflect real performance in your application.

So the best answer is to measure and compare the performance in the context of your actual application. In other words:

  1. Get your application working
  2. Write a benchmark for measuring your application's performance with realistic test data / inputs
  3. Use the benchmark to compare the performance of the two alternatives in the context of your application.

(Your question has the smell of premature optimization about it. You should be able to put off deciding which of these alternatives is better ... until you have the tools to make a well-founded decision.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.