• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Are 2 arrays more costly than 1 in 2D?

 
Elouise Kivineva
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I need to read 2 or 3 sets of values I prefer to use different arrays instead of a two- or three-dimentional array. Is this somehow more costly (efficiency, memory) or is it basically OK?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd choose whichever makes the most sense to the structure of your program and not worry about which would be marginally more efficient than the other.
See Jamie Robertson's reply to this topic. Pretty much sums it up.
hth,
bear
[ October 29, 2002: Message edited by: Bear Bibeault ]
 
Elouise Kivineva
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point to consider but what I really wanted to know is does one run more slowly or use more memory than the other?
 
Graeme Brown
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The performance differences, if any, are insignificant and will vary between versions of compiler and JVM.
Think more about whether your code will be easier to read, understand, and maintain.
 
Sayed Ibrahim Hashimi
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Graeme Brown:
The performance differences, if any, are insignificant and will vary between versions of compiler and JVM.
Think more about whether your code will be easier to read, understand, and maintain.

I totally agree.
 
Michael Zalewski
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand your question correctly, you would like to know which is more efficient:

or

I think the answer is probably the first way. It creates 3n String objects, and 3 String[] objects. (assuming all the elements are populated).
The second way still has 3n String objects (to be populated). It also has 1 String[][] object, and n String[] objects. The first way creates 3n + 3 objects. The second way creates 4n + 1 objects. I may not be counting one more for the String[][].class, not sure about that one.
The overhead is probably not significant. We probably spent more time reading these messages than billions of iterations of your program will save in CPU time. So like Graeme Brown said, I would code it whichever way was easier to understand and adapt to change.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic