Comparison of hash tables and lists in C #

Source: Internet
Author: User

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.

  1. /*
  2. *author:chao
  3. *date:2012.3.27
  4. *fun:compare list with hashes in C #
  5. */
  6. Using System;
  7. Using System.Collections.Generic;
  8. Using System.Linq;
  9. Using System.Text;
  10. Using System.Collections;
  11. Namespace Hash_vs_list
  12. {
  13. Class Program
  14. {
  15. const int MAX = 50000;
  16. static void Main (string[] args)
  17. {
  18. //hash_table
  19. Console.WriteLine ("hash_table");
  20. TimeSpan Hash_start = new TimeSpan (DateTime.Now.Ticks); //Start time
  21. Hashtable H1 = new Hashtable ();
  22. For (int i = 0; i < MAX; i++)
  23. H1. ADD (i, i.tostring ());
  24. TimeSpan hash_end = new TimeSpan (DateTime.Now.Ticks); //End time
  25. TimeSpan Hash_diff = Hash_start. Subtract (Hash_end). Duration (); //Calculate time difference
  26. string hash_create_diff = Hash_diff.   Totalseconds.tostring (); //     
  27. //hashtable Search
  28. Hash_start = new TimeSpan (DateTime.Now.Ticks);
  29. string tmp = H1[0].  ToString ();
  30. Hash_end = new TimeSpan (DateTime.Now.Ticks);
  31. Hash_diff = Hash_start. Subtract (Hash_end). Duration ();
  32. string hash_search_diff = Hash_diff.  Totalseconds.tostring ();
  33. Console.WriteLine ("create:{0} search:{1}", Hash_create_diff, Hash_search_diff);
  34. Console.WriteLine ();
  35. //dict
  36. Console.WriteLine ("dictionary");
  37. dictionary<int, string> dict = new dictionary<int, string> ();
  38. TimeSpan Dict_start = new TimeSpan (DateTime.Now.Ticks);
  39. For (int i = 0; i < MAX; i++)
  40. {
  41. Dict. ADD (i, i.tostring ());
  42. }
  43. TimeSpan dict_end = new TimeSpan (DateTime.Now.Ticks);
  44. TimeSpan Dict_diff = Dict_start. Subtract (Dict_end). Duration ();
  45. string dict_create_diff = Dict_diff.  Totalseconds.tostring ();
  46. //dict Search
  47. Dict_start = new TimeSpan (DateTime.Now.Ticks);
  48. TMP = dict[0];
  49. Dict_end = new TimeSpan (DateTime.Now.Ticks);
  50. Dict_diff = Dict_start. Subtract (Dict_end). Duration ();
  51. string dict_search_diff = Dict_diff.  Totalseconds.tostring ();
  52. Console.WriteLine ("create:{0} search:{1}", Dict_create_diff, Dict_search_diff);
  53. Console.WriteLine ();
  54. //list Create
  55. Console.WriteLine ("List");
  56. TimeSpan List_start = new TimeSpan (DateTime.Now.Ticks);
  57. list<int> l1 = new list<int> ();
  58. For (int i = 0; i < MAX; i++)
  59. L1. ADD (i);
  60. TimeSpan list_end = new TimeSpan (DateTime.Now.Ticks);
  61. TimeSpan List_diff = List_start. Subtract (List_end). Duration ();
  62. string list_create_diff = List_diff.  Totalseconds.tostring ();
  63. //list foreach
  64. List_start = new TimeSpan (DateTime.Now.Ticks);
  65. foreach (int i in L1)
  66. {
  67. if (i = = MAX)
  68. Break ;
  69. }
  70. List_end = new TimeSpan (DateTime.Now.Ticks);
  71. List_diff = List_start. Subtract (List_end). Duration ();
  72. string list_foreach_diff = List_diff.  Totalseconds.tostring ();
  73. Console.WriteLine ("Create:{0},foreach:{1}", List_create_diff, List_foreach_diff);
  74. Console.WriteLine ();
  75. Console.read ();
  76. }
  77. }
  78. }

Comparison of hash tables and lists in C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.