• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Are 2 arrays more costly than 1 in 2D?

 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic