变量访问性能
我正在开发一个具有 DataManager 类的应用程序,该类包含一个 ArrayList
A) 创建一个 public static ArrayList
B) 创建一个 public ArrayList
方法以及其他类中的方法创建局部变量 ArrayList
C)..?
在我看来,B 由于对象创建而有更多的开销。另外我读静态比非静态更快?
I'm developing an app that has a DataManager class, which holds an ArrayList<Object[]>
. As this ArrayList needs to be used within other classes, I am wondering what would be the most efficient and fastest way of accessing this list, considering this application will be running on the Android platform.
A) create a public static ArrayList<Object[]> data
in the DataManager class and reference it within other classes through DataManager.data
B) create a public ArrayList<Object[]> getData
method within the DataManager class and have methods within other classes create local variable ArrayList<Object[]> data = mDataManager.getData()
for temporary use.
C) ..?
It seems to me B has more overhead due to object creation. Also I read static is faster than non-static?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
选项 B 不会增加内存使用,因为您只有一个 ArrayList 对象(使用它的所有对象都只保存一个简单的引用,而不是副本)。使用 ArrayList 的对象还可以将此引用存储为实例变量,而不是每次需要时都从管理器类请求它。
我在某处读到访问实例变量比访问类(
static
)变量稍快,但我没有源代码的链接。性能差异可能没有意义。然而,选项 B 为您提供了更好的封装。
Option B does not increase memory use, since you will only have one
ArrayList
object (all the objects that use it just hold a simple reference, not a copy). The objects that use theArrayList
could also store this reference as an instance variable, instead of requesting it from the manager class each time it is needed.I read somewhere that access to instance variables is slightly faster than accessing class (
static
) variables, but I don't have the link to the source.The difference in performance is not likely to be meaningful. However, Option B gives you better encapsulation.