aspose file tools*
The moose likes Beginning Java and the fly likes Are 2 arrays more costly than 1 in 2D? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Are 2 arrays more costly than 1 in 2D?" Watch "Are 2 arrays more costly than 1 in 2D?" New topic
Author

Are 2 arrays more costly than 1 in 2D?

Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
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

Joined: Jan 10, 2002
Posts: 61198
    
  66

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
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

Joined: Oct 13, 2000
Posts: 193
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

Joined: May 17, 2001
Posts: 148
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.


SCJP 1.4<br /><a href="http://www.cise.ufl.edu/~sih" target="_blank" rel="nofollow">www.cise.ufl.edu/~sih</a>
Michael Zalewski
Ranch Hand

Joined: Apr 23, 2002
Posts: 168
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Are 2 arrays more costly than 1 in 2D?