Android 开发者指南提到了以下几点
If you need to implement a container that stores tuples of (Foo,Bar) objects, try to remember that two parallel
Foo[]
andBar[]
arrays are generally much better than a single array of custom (Foo,Bar) objects.
谁能帮我理解为什么使用两个并行数组会更快?
最佳答案
我假设您正在阅读此页面:https://developer.android.com/training/articles/perf-tips
他们所说的背后的想法是每个对象实例都会产生少量的内存开销。因此,一个由许多 Tuple
对象组成的数组,每个对象包含一个 Foo
和一个 Bar
将比两个数组(一个 Foo
对象和 Bar
对象之一)。
这种内存消耗的增加可能会导致垃圾收集器更频繁地运行,并且每个垃圾收集操作都会暂停您的应用程序(尽管它只会暂停一小部分秒)。
但是,请记住这篇文章非常旧。您可以从这一行(强调我的)中准确了解它的年龄:
The concurrent garbage collector introduced in Android 2.3 helps
当 Android 硬件较弱且速度较慢时,这些性能考虑因素更为重要。现在我们正在研究功能更强大几个数量级的硬件,此类事情的实际影响已大大降低。
一般来说,我非常推荐反对采纳这篇文章的建议。一组 Tuple
对象更更容易推理,并且从长远来看将使您的应用程序更容易维护。除非您已经完成了具体和明确的性能测量并且毫无疑问地确定您的应用程序过于频繁地进行垃圾收集,否则请忽略这些“提示”。
事实上,即使这篇文章告诉您,您也应该忽略它,除非您知道您有问题:
Choosing the right algorithms and data structures should always be your priority
和
Always measure
Before you start optimizing, make sure you have a problem that you need to solve. Make sure you can accurately measure your existing performance, or you won't be able to measure the benefit of the alternatives you try.
关于android - 为什么两个并行数组比单个数组存储元组更快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52503127/