1

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

2
  • Do you have some code sample we can test? Commented Feb 23, 2009 at 18:56
  • What are you trying to achieve? If you have performance problems it is probably not due to the way you organize your arrays. Commented Feb 23, 2009 at 19:10

6 Answers 6

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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: try a profiler before trying to optimize arrays. What are you doing with the arrays?
1

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.

2 Comments

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.
The overhead of having an object will be more significant than the overhead of the reference to it, in both memory and CPU.
1

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.

1 Comment

Yeah, true even for "real" multidimensional arrays.
0

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.

1 Comment

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?
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.