Results 1 to 6 of 6
  1. #1
    MasterCRB is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Which is faster?

    I have a collection of nodes which I need to check.

    Would it be faster to iterate over the collection to find what I'm looking for, or copy the collection to a hashmap and then use a hash function to find the node straight away?

    Thanks!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Which is faster?

    It depends on how many items you have. A map still has to iterate over some particular bucket of information. If your collection is quite large then a hashMap on average will probably be faster. If your collection is stable and it doesn't grow very fast, then keeping it sorted would lend itself to a binary search which can be very fast. But if you don't access it often that could be more overhead than is worth it. You could also try some test cases and see how each one performs.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,532
    Blog Entries
    7
    Rep Power
    20

    Default Re: Which is faster?

    If you have to check each and every node, iterating over your original collection is the way to go; i.e. hopping from one element to the next takes only O(1) operations.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Which is faster?

    It might also help to pick the correct collection class; for example use an ArrayList rather than a LinkedList since an ArrayList is sequential in memory, while the nodes of a LinkedList will be very fragmented.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,532
    Blog Entries
    7
    Rep Power
    20

    Default Re: Which is faster?

    Quote Originally Posted by gimbal2 View Post
    It might also help to pick the correct collection class; for example use an ArrayList rather than a LinkedList since an ArrayList is sequential in memory, while the nodes of a LinkedList will be very fragmented.
    ... and hopping over a linked list in an indexed way is just stupid; better use an Iterator over the thing.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    101
    Rep Power
    0

    Default Re: Which is faster?

    faster to iterate over the list... the first option imo

Similar Threads

  1. My A* Algorithm(code), How can I make it faster?
    By 0circle0 in forum New To Java
    Replies: 13
    Last Post: 07-06-2011, 11:53 PM
  2. replace string, what is faster
    By henry123 in forum Advanced Java
    Replies: 12
    Last Post: 03-02-2011, 09:57 AM
  3. Which is faster?
    By Dan0100 in forum New To Java
    Replies: 19
    Last Post: 09-26-2010, 09:53 PM
  4. How to make my download class faster
    By pietertje in forum New To Java
    Replies: 3
    Last Post: 08-07-2010, 11:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •