4

I have a design choice to make: Do I create an array of wrapper objects each containing a few distinct values, or do I create an object that contains a few distinct arrays of values?

Option 1:

Node[][] nodes;
class Node{
   double val1;
   double val2;
}

Option 2:

Node[] nodes;
class Node{
    double[] val1;
    double[] val2;
}

My gut says that option 2 would be more efficient only because there would be fewer objects and thus less overhead, but would the double[]'s be just as expensive?

2
  • 6
    Design wise, first consider Node class and design it in such a way that the variables make most sense with the overall semantics. Then worry about efficiency. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil - Donald Knuth Commented Aug 11, 2011 at 19:28
  • Why not create an Interface for your array and have your arrays implement it, that way if for some reason one array does not meet your requirements you'll easily be able to sub it for the other. Commented Aug 11, 2011 at 20:15

3 Answers 3

7

Do you know that there will be a significant issue here? How many of these are you going to create?

You shouldn't worry too much about the performance to start with - ask yourself whether a single node logically has multiple pairs of values or just a single pair. Let your classes follow what you're modelling - keep an eye on performance and memory usage, but don't let it dictate your design to the exclusion of a natural model.

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

3 Comments

I was in the middle of writing an answer... but I bow before your superior knowledge instead.
I don't expect it to be a large issue, but either way seems to be a reasonable model of thinking so I figured pick the one that is more efficient.
@Andrew: One is likely to be superior to the other in terms of modelling - but we don't know enough about your domain to give you any advice on that front. A small difference in being pleasant to use is likely to be much more significant than a small difference in memory usage :)
1

Memory model ->

Array = value1, value2, value3 ...

Object = Field1, Field2, Field3...

If you have array of objects the memory looks like: Field1, Field2, Field3, Field1, Field2, Field3...

If you have an object with arrays the memory looks like Field1, Field1, Field1.... Field2, Field2, Field2...

Access to contiguous memory is faster than access to non-contiguous memory.

Comments

1

If you have an array of 10*20, it means 10*20*2 in the first case and 10*(20+20) in the second case. In both cases, that makes 400. So there is no difference in terms of memory.

If your array is only containing a couple of nodes, you may as well consider a HashMap where K is an immutable class containing the array coordinates of a given node and V an object containing val1 and val2 for that node. You would only allocate memory per node, not for the whole array.

2 Comments

While both would require space for 400 doubles, Option 1 would have 200 Nodes, and option 2 would have 20 Nodes (the first array is being moved inside). Though in practice the array sizes will be more along the lines of [3][1000+]. So I think ultimately it would make little difference. Thanks.
@Andrew Another solution is to create two 10*20 arrays of doubles and not create nodes at all. It saves even more memory.

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.