![]() ![]() If we wanted to identify books whose title contains a particular string, the hash map would be of little use. Hash maps work well when we know exactly what we are looking for. ![]() However, for millions of books, the performance differences are clearly visible.ĭoes this mean that we'll only be using hash maps going forward? Of course not. The difference in performance depends on the number of books - for example, the performance differences are negligible for 10 books. In a hash map, it isn't necessary to check all of the books as the key determines the location of a given book in a hash map. ![]() The difference in performance is due to the fact that when a book is searched for in a list, the worst-case scenario involves going through all the books in the list. The difference in performance in our example is over a thousandfold. It took about 0.4 milliseconds to search for two books out of ten million books with the hash map. The book search took 0.411458 milliseconds. In the example below, the books have been stored in a list and searching for them is done by going through the list. In that case, the books would be placed on the list instead of the directory, and the book search would happen by iterating over the list. The whole program could just as well have been implemented using a list. We'll be taking a deeper look into the implementation of a hash map in the Advanced Programming and Data Structures and Algorithms courses.Ĭonsider the library example that was introduced above. In practice, it's not necessary to go through all the key-value pairs in the hash map when searching for a key the set that's checked is significantly smaller. When a key is used to retrieve information from a hash map, this particular code identifies the location where the value associated with the key is. a piece of code, which is used to store the value of a specific location. The hash map generates a "hash value" from the key, i.e. The hash map is implemented internally in such a way that searching by a key is very fast. Log in to view the quiz When Should Hash Maps Be Used? ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |