问题描述
限时送ChatGPT账号..我想知道,您可以在现有 ArrayList
上调用的 size()
方法是否已缓存?或者在性能关键代码中我只是将 size()
存储在本地 int 中更可取?
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?
我希望它确实被缓存,当您不在对 size()
的调用之间添加/删除项目时.
I would expect that it is indeed cached, when you don't add/remove items between calls to size()
.
我说得对吗?
更新
我不是在谈论内联或诸如此类的事情.我只想知道方法 size()
本身是在内部缓存该值,还是每次调用时动态计算.
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.
size()
的 Sun JDK 实现只是:
The Sun JDK implementation of size()
is just:
public int size() {
return size;
}
这篇关于ArrayList.size() 方法是否被缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论