Youssef Shoaib [MOD]
11/14/2023, 7:24 PMdata class AppendableArray<T>(array: Array<T>, freeIndex: Int)
? Or is that close enough in performance to a List, and so a List makes more sense because of ergonomics?
In other words, I'm creating a List of exactly size N, but I'm adding the elements recursively, and so I'm not really building them based on the index. Is an Array + lastIndex "better" in any way, or is a List with initial capacity close enough in performanceJoffrey
11/14/2023, 7:26 PMJoffrey
11/14/2023, 7:27 PMYoussef Shoaib [MOD]
11/14/2023, 7:28 PMJoffrey
11/14/2023, 7:34 PMArrayList
is technically a wrapper over an array+index, that replaces the array with a bigger one when needed. If you tune the initial capacity so it never resizes the array, I don't believe there should be a significant performance difference when writing data. There could be some subtleties related to indirections of using the list wrapper to edit the array (e.g. memory locality), but I'm not sure this would have a noticeable impact on perf (also if you write your own wrapper, you have the same problem). One big difference of course would be using List<Int>
vs IntArray
, but that only applies to primitives.Youssef Shoaib [MOD]
11/14/2023, 7:38 PMArrayList<IntArray>
Vs Array<IntArray> + some mutable index
would matter too much. This was a purely academic exercise, so my question is entirely theoretical.ephemient
11/14/2023, 7:39 PMjava.util.ArrayList
contains fields Object[] data
, int size
, and int modCount
from its superclass AbstractList
ephemient
11/14/2023, 7:40 PMkotlin.collections.ArrayList
is basically an alias on JVMJoffrey
11/14/2023, 7:45 PMI was wondering if having an ArrayList<IntArray> Vs Array<IntArray> + some mutable index would matter too muchIf you compare
ArrayList
to your wrapper class (and not to the direct usage of the array itself), then it's pretty much the sameKlitos Kyriacou
11/15/2023, 9:06 AMArrayList
it would check its capacity, which your custom class wouldn't do because it already knows it has the capacity. But I don't think you'd be able to measure that difference.