Pages

Showing posts with label performance. Show all posts
Showing posts with label performance. Show all posts

Tuesday, February 22, 2011

Hash - Eternally Confuzzled


A hash table, put simply, is an abstraction of an array that allows any value to be used as an index. While an array requires that indices be integers, a hash table can use a floating-point value, a string, another array, or even a structure as the index. This index is called the key, and the contents of the array element at that index is called the value. So a hash table is a data structure that stores key/value pairs and can be quickly searched by the key. Because insertion and removal are operations dependent on the speed of the search, they tend to be fast as well.
To achieve this magic, a hash table uses a helper function that converts any object into an integral index suitable for subscripting the array. 

use hash to improve the performance

two arrays, too loops, usually the complex will be O(n*n).
but if we use a hash dict to store one array, then we can go through every item in the array filter by the hash dict directly, that will be very fast, and the complex only be O(n+n).