• 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

Use array or Map?

 
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have an odd question, but it's quite important for the chess program I'm making.
I need to know which of the following two options executes faster:

1. int number=someArray[x][y];

2. int number=((Integer)someMap.get(new Long(key))).intValue();

The problem with collections is needing to cast, then cast back.
So I'm guessing second option is slower, but without all the casting, would this be quicker than option 1:

3. Integer number=(Integer)someMap.(KEY); where KEY is a Long.

Thanks for any help
[ April 15, 2006: Message edited by: Bear Bibeault ]
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that accessing a direct memory address in an array should always be faster than looking for a key in Map.

However, arrays are of immutable size, while Maps can grow dynamically, you have to take that into account.

On the other hand, If you use JDK 1.5 (Tiger) you can avoid casting, at least at compile time.



Best regards,
Edwin Dalorzo.
[ April 15, 2006: Message edited by: Edwin Dalorzo ]
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try using the System.currentMillis() and System.timeNanos() methods.
Set a long number to the time before and after you run your method, and print out the difference. It will probably only be a microsecond or so, so you will probably find the nanosecond method more useful.


If it doesn't work, it is because I have given you the wrong name for the methods.


CR

And I have gone back to the post to add:

Remember to tell us which works faster.
[ April 15, 2006: Message edited by: Campbell Ritchie ]
 
Sheriff
Posts: 67746
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
Moved to the intermediate forum.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good reasons to use Map: You don't know the size of the collection ahead of time, the key is not an integer but maybe something like state abbreviation, the key is a number but there will be many gaps in the key sequence.

Good reasons to use index: The key is an integer, you won't skip any keys, you need fast access sequentially, maybe reverse order as well as random by key.

I'm kinda making those up as I go. Do try the timing comparison and see if I was right or if it makes much difference in your situation. And hide the decision within some class so you can change it in the future without breaking any other code.
 
reply
    Bookmark Topic Watch Topic
  • New Topic