Simple concept
In C #, list is a sequential linear table (non-linked list) that stores the linear structure of data elements in sequence with a contiguous set of storage cells.
A hash table, also known as a hash table, is a data structure that accesses records by mapping key code values to a location in the tables. The hash table in C # has hashtable,dictionary,hashtable inherited from map, which implements a key-value mapping relationship. Dictionary is a generic hash table, unlike Hashtable's key unordered, dictionary is stored in order. The hash table is characterized by: 1. Find Fast, 2. Cannot have duplicate key.
Create a process
In C #, when we instantiate a list, if we do not specify a capacity, a static empty array is generated internally, and when added, it is instantiated as an array of length 4, and automatically expands to twice times the capacity after it is full.
A hash table is a similar case, the default is to generate a hash table length of 11, full, automatically add, and add the rule is an array of internal loop primes, slightly larger than the current length, such as 15, the length is set to 17. The hash table creation can be divided into deterministic hash functions, which resolve the two steps of the SID conflict.
Experimental execution results
The following two experiments, experiment one, compare the Hashtable,dictionary,list three at different lengths of the creation speed, experimental two comparison of three at different times of the search speed. (Experimental code at the end)
Hardware: Intel Core 2 Quad CPU @ 2.66GHZ,4GB memory.
Software: Windows 2008,2010 vs compilation environment.
Table 1. Creation time of the three
Length \ Type |
Hashtable |
Dictionary |
List |
1 |
0.001s |
0s |
0s |
10 |
0S |
0S |
0s |
100 |
0S |
0S |
0s |
1000 |
0.003s |
0.005s |
0.001s |
10000 |
0.0032s |
0.003s |
0.002s |
100000 |
0.038s |
0.042s |
0.002s |
1000000 |
0.520s |
0.512s |
0.015s |
3000000 |
1.807s |
1.441s |
0.042s |
6000000 |
3.752s |
2.952s |
0.087s |
8000000 |
4.744s |
3.740s |
0.102s |
Table 2. Search Time for three persons
Length \ Type |
Hashtable |
Dictionary |
List |
1 |
0s |
0s |
0s |
10 |
0s |
0s |
0s |
100 |
0s |
0s |
0s |
1000 |
0s |
0s |
0s |
10000 |
0s |
0s |
0.001s |
100000 |
0s |
0s |
0.010s |
1000000 |
0s |
0s |
0.009s |
3000000 |
0s |
0s |
0.009s |
6000000 |
0s |
0s |
0.058s |
8000000 |
0s |
0s |
0.077s |
Summarize
The experiment shows that the hash table takes more time to build the data structure than the list, because it takes time to resolve the hash conflict, and the list does not need it. However, it is important to note that the setup operation only needs to be performed once.
The second experiment shows that the lookup speed of a hash table is hardly time consuming, because the hash table already records the position of value when it adds a pair of elements (Key-value), so it is very fast to find. The lookup complexity of the hash is O (1), and the lookup complexity of the list is O (n).
The high-speed lookup of a hash table is a typical application of space-changing time, and the settling time increases with the order of magnitude, and the later search is always O (1). So we came to the conclusion that it is recommended to use a hash table if you face massive amounts of data and require a lot of searching. It is more appropriate to use list when faced with a small amount of data (the order of magnitude is determined by the server).
The last friendship reminds: first, although in the search on the hash occupies an advantage, but insufficient is 1. Occupy Space is big, 2. Cannot have duplicate health. Second, if you need to use a hash table, it is recommended to use dictionary more. Dictionary runs slightly faster than Hashtable, and dictionary is type-safe, which helps us write more robust and readable code, eliminating the hassle of forcing conversions. Dictionary is generic, and when K or V is a value type, it is much faster than Hashtable.
- /*
- *author:chao
- *date:2012.3.27
- *fun:compare list with hashes in C #
- */
- Using System;
- Using System.Collections.Generic;
- Using System.Linq;
- Using System.Text;
- Using System.Collections;
- Namespace Hash_vs_list
- {
- Class Program
- {
- const int MAX = 50000;
- static void Main (string[] args)
- {
- //hash_table
- Console.WriteLine ("hash_table");
- TimeSpan Hash_start = new TimeSpan (DateTime.Now.Ticks); //Start time
- Hashtable H1 = new Hashtable ();
- For (int i = 0; i < MAX; i++)
- H1. ADD (i, i.tostring ());
- TimeSpan hash_end = new TimeSpan (DateTime.Now.Ticks); //End time
- TimeSpan Hash_diff = Hash_start. Subtract (Hash_end). Duration (); //Calculate time difference
- string hash_create_diff = Hash_diff. Totalseconds.tostring (); //
- //hashtable Search
- Hash_start = new TimeSpan (DateTime.Now.Ticks);
- string tmp = H1[0]. ToString ();
- Hash_end = new TimeSpan (DateTime.Now.Ticks);
- Hash_diff = Hash_start. Subtract (Hash_end). Duration ();
- string hash_search_diff = Hash_diff. Totalseconds.tostring ();
- Console.WriteLine ("create:{0} search:{1}", Hash_create_diff, Hash_search_diff);
- Console.WriteLine ();
- //dict
- Console.WriteLine ("dictionary");
- dictionary<int, string> dict = new dictionary<int, string> ();
- TimeSpan Dict_start = new TimeSpan (DateTime.Now.Ticks);
- For (int i = 0; i < MAX; i++)
- {
- Dict. ADD (i, i.tostring ());
- }
- TimeSpan dict_end = new TimeSpan (DateTime.Now.Ticks);
- TimeSpan Dict_diff = Dict_start. Subtract (Dict_end). Duration ();
- string dict_create_diff = Dict_diff. Totalseconds.tostring ();
- //dict Search
- Dict_start = new TimeSpan (DateTime.Now.Ticks);
- TMP = dict[0];
- Dict_end = new TimeSpan (DateTime.Now.Ticks);
- Dict_diff = Dict_start. Subtract (Dict_end). Duration ();
- string dict_search_diff = Dict_diff. Totalseconds.tostring ();
- Console.WriteLine ("create:{0} search:{1}", Dict_create_diff, Dict_search_diff);
- Console.WriteLine ();
- //list Create
- Console.WriteLine ("List");
- TimeSpan List_start = new TimeSpan (DateTime.Now.Ticks);
- list<int> l1 = new list<int> ();
- For (int i = 0; i < MAX; i++)
- L1. ADD (i);
- TimeSpan list_end = new TimeSpan (DateTime.Now.Ticks);
- TimeSpan List_diff = List_start. Subtract (List_end). Duration ();
- string list_create_diff = List_diff. Totalseconds.tostring ();
- //list foreach
- List_start = new TimeSpan (DateTime.Now.Ticks);
- foreach (int i in L1)
- {
- if (i = = MAX)
- Break ;
- }
- List_end = new TimeSpan (DateTime.Now.Ticks);
- List_diff = List_start. Subtract (List_end). Duration ();
- string list_foreach_diff = List_diff. Totalseconds.tostring ();
- Console.WriteLine ("Create:{0},foreach:{1}", List_create_diff, List_foreach_diff);
- Console.WriteLine ();
- Console.read ();
- }
- }
- }
Comparison of hash tables and lists in C #