views:

256

answers:

6

For example, would a full int[50][8] use more resources (RAM and CPU) than 8 full int[50] arrays?

+3  A: 

In the first case you have one array object pointing to fifty array objects holding 8 int's. So 1 + 50 array objects + fifty pointers in the first array object.

In the second case you have one array object pointing to 8 array objects holding 50 int's. So 1 + 8 array objects + eight pointers in the first array object. Holding the int's is a wash.

There is not a good way to evaluate CPU usage for this.

Clint
So which one, if any, is better for CPU? Cause I'm having wicked CPU problems on my Java project, and the only thing I can think of is the array.
William
@William: try a profiler before trying to optimize arrays. What are you doing with the arrays?
Michael Myers
A: 

There appears to be three things to compare here.

  • new int[50][8]
  • new int[8][50]
  • new int[400]

Now, I get this confused, but the way to remember is to think of new int[50][] which is valid.

So new int[50][8] is an array of 50 arrays of size 8 (51 objects). new int[8][50] is an array of 8 arrays of size 50 (9 objects). 9 objects will have a lower overhead than 51. new int[400] is just one object.

However, it at this size it probably doesn't make any measurable difference to the performance of your program. You might want to encapsulate the array(s) within an object that will allow you to change the implementation and provide a more natural interface to client code.

Tom Hawtin - tackline
you're missing the 8 separate arrays for each of the 50 objects. it's all about having something to store 8 lists of variables in, not 50.
William
The overhead of having an object will be more significant than the overhead of the reference to it, in both memory and CPU.
Tom Hawtin - tackline
A: 

you could tweak a tiny amout of memory by using an int[] myInt = int[400] array, and manually accessing an int at position (x,y) with myInt[x+y*50] that would save you 50 32-bit pieces of memory. accessing it that way will maybe (who knows exactly what the hotspot compiler does to this..) take one more instruction for the multiplication.

that kind of micro-optimisation will most likely not make your app perform better, and it will decrease readability.

Andreas Petersson
I'm not an expert at the Java compiler, but this seems like something easy the compiler would probably do for you anyway. Does anyone know if this is the case?
Brad Barker
A: 

One additional useage point (came from a reference I unfortunately can't find now, but fairly commonsensical)-

The authors of this paper were testing various ways of compressing sparse arrays into mutidimensional arrays. One thing they noticed is that it makes a difference in terms of speed which way you iterate -

The idea was that if you have int[i][j] it was faster to do

for (i) { 
     for (j)

than to do

for (j) { 
     for (i)

because in the first instance you're iterating through elements stored contiguously.

Steve B.
Yeah, true even for "real" multidimensional arrays.
Tom Hawtin - tackline
A: 

I suggest writing a small performance test for this with very large arrays to see the actual difference. In reality I don't think this would make the slightest difference.

Stefan Thyberg
A: 

int[50][8] is 50 arrays of length 8 int[8][50] is 8 arrays of length 50 int[400] is one array 400. Each array has an overhead of about 16 bytes.

However, for the sizes you have here, it really doesn't matter. You are not going to be saving much either way.

Peter Lawrey