Busti
Busti

Reputation: 5604

Is a single array faster than 2 different arrays?

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};

Upvotes: 6

Views: 1760

Answers (4)

Tobias
Tobias

Reputation: 7771

Instantiation

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.

Access

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.

Memory

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.

Caching

Access to two different objects can obviously be slower. Not much to say about this. One object is definitely preferable.

Variable access & stack

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.

Conclusion

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.

Upvotes: 4

afsantos
afsantos

Reputation: 5208

The single array is probably more efficient:

  • A tiny bit less memory;
  • Only one object on cache.

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.

Upvotes: 1

Tim B
Tim B

Reputation: 41188

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.

Upvotes: 4

fedorSmirnov
fedorSmirnov

Reputation: 711

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.

Upvotes: 2

Related Questions