Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use array or Map?

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • 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 ]
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • 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 ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 48976
60
  • Mark post as helpful
  • send pies
  • 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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the intermediate forum.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic