I was wondering if it is more efficient to have one single array store some kind of data than having multiple arrays storing the same information.
int a1;
int a2;
int b1;
int b2;
int array1[] = {a1, b1, a2, b2}; // Is this faster than 2 arrays?
int array2A[] = {a1, a2};
int array2B[] = {b1, b2};
The two arrays are potentially going to have worse cache cohesion (as they in different parts of memory). They will use a tiny amount more memory. The gains are very unlikely to be worth it though. Focus on making your program as good and well architectured as you can instead of worrying about tiny potential gains like this and you will get much better results.
Well, you can argue about this. Instantiating array1
takes 3 bytecode instructions (bipush
, newarray
, astore
), instantiating array2X
takes 3 bytecode instructions per array (bipush
/ iconst
, newarray
, astore
). Speaking theoretically, instantiating array1
could be faster.
This is what the VM does to get array1[2]
:
aload_1 ; array1
iconst_2 ; index
iaload ; value
Guess what it does to get array2B[0]
:
aload_3 ; array2B on stack
iconst_0 ; index
iaload ; value
No points for anybody.
The size of the arrays is equal (array1
is as large as array2A
+ array2B
), at least the size of the contents. Yeah, there are two pointers instead of one, some GC overhead and so on. Don't think about this too much, you won't be able to measure the difference.
Access to two different objects can obviously be slower. Not much to say about this. One object is definitely preferable.
Using a single array might improve the overall performance because it can be kept on the stack sometimes. However, the JDK compiler seems to avoid stack operations and falls back to variables.
There won't be a noticeable difference (although array1
will usually be faster). All of the above points depend on the VM implementation.
Write good code, don't over-optimize.
I think that the access of an element is constant time in both cases. With one array however, you need only one pointer to the array head, so in terms of memory, you could say that the one array implementation is more efficient.
The single array is probably more efficient:
However, I seriously doubt the gains will outweight the cons, and I also doubt the performance gain (if any) will be noticeable, or make any significant difference.
If you end up calculating indexes frequently, you may even lose performance. Take the following examples:
// With two arrays
void sumBToA() {
assert array2A.length == array2B.length;
for (int i = 0; i < array2A.length; ++i) {
array2A[i] = array2A[i] + array2B[i];
}
}
// With one array
void sumBToA() {
for (int i = 0; i < array1.length; i += 2) {
array1[i] = array1[i] + array1[i + 1];
}
}
It's two memory accesses versus two memory accesses and an index calculation. Again, this probably won't make any significant difference, under most circumstances.
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