I was wondering, is the size()
method that you can call on a existing ArrayList<T>
cached?
Or is it preferable in performance critical code that I just store the size()
in a local int?
I would expect that it is indeed cached, when you don't add/remove items between calls to size()
.
Am I right?
update
I am not talking about inlining or such things. I just want to know if the method size()
itself caches the value internally, or that it dynamically computes every time when called.
I don't think I'd say it's "cached" as such - but it's just stored in a field, so it's fast enough to call frequently.
The Sun JDK implementation of size()
is just:
public int size() {
return size;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With