C # ordinary dictionary, Dictionary concurrent (Concurrent) and read and write performance comparison HashTable

C # ordinary dictionary, Dictionary concurrent (Concurrent) and read and write performance comparison HashTable

Recent improvements in the efficiency of the program do to speed up the examination procedure to check the time, so sometimes have to consider switching to more efficient container to reduce the running time. Reference to the article (Click to view original), based on this, I had to modify, add to the reading and writing classes and HashTable.

Sometimes it is necessary concurrent multithreading, multi-threaded read things in the same container is fine, but if you need to modify the questions and write to the same container, the index will fail, that the two processes at the same time to the same write to a position, in this case need to lock (var) , to lock the container, it can also be used directly concurrent read and write of the container, such as ConCurrentDictionary.

Thus, part 2 test points, one is a write operation, no lock comprising a lock write and write, wherein each of which is subdivided into a class written and write the string, there is a traversing operation, also includes a lock no lock read and read, and which also read the string read into classes.

Test results:
more common dictionaries, dictionaries and concurrent HashTable efficiency, with 10 million to do the test data, the time in milliseconds.
Experimental results

1. When a writing :
Here Insert Picture Description
Here Insert Picture Description
2. traversal Time:
Here Insert Picture Description
Here Insert Picture Description
Conclusion:

  • For a write operation speed: Normal Dictionary> HashTable> concurrency Dictionary
  • For a read operation speed: Concurrent Dictionary> locking Dictionary> HashTable
  • Whether ordinary dictionary or HashTable, the time it takes to have a lock slower than no lock, for thread safety, it must sacrifice time.
    So if you need to write your own, then I recommend locking the ordinary dictionary, read and write speeds are very balanced.

Original authors explain:

concurrentDictionary using fine-grained locking [fine-grained locking]
general dictionary using a coarse-grained interlocking locking [coarse-grained locking]
In the case of a multi-core multithreaded concurrentDictionary will have better performance

Experiment code is as follows:

    class Program
    {
        public class student
        {
            public string name;
            public int age;
        }
        static void Main(string[] args)
        {
            var concurrentDictionary = new ConcurrentDictionary<int, string>();
            var concurrentDictionaryClass = new ConcurrentDictionary<int, student>();
            var dictionary = new Dictionary<int, string>();
            var dictionaryClass = new Dictionary<int, student>();
            Hashtable ht = new Hashtable();
            Hashtable htClass = new Hashtable();
            string CurrentItem = "";
            student stu = new student { name = Item, age = 333 };
            var sw = new Stopwatch();
            //写入字典 无锁
            sw.Start();
            for (int i = 0; i < 10000000; i++)
            {
                    dictionary[i] = Item;
            }
            sw.Stop();
            Console.WriteLine("wrinting to dictionary WithOUT a lock: {0}", sw.Elapsed.TotalMilliseconds);


            //写入字典 带锁
            dictionary = new Dictionary<int, string>();
            sw.Restart();
            for (int i = 0; i < 10000000; i++)
            {
                lock (dictionary)
                {
                    dictionary[i] = Item;
                }
            }
            sw.Stop();
            Console.WriteLine("wrinting to dictionary with a lock: {0}", sw.Elapsed.TotalMilliseconds);


            //类写入字典 无锁
            sw.Restart();
            for (int i = 0; i < 10000000; i++)
            {
                
                dictionaryClass[i] = stu;
            }
            sw.Stop();
            Console.WriteLine("wrinting  CLASS to dictionaryClass WITHOUT a lock: {0}", sw.Elapsed.TotalMilliseconds);


            //类写入字典 带锁
            dictionaryClass = new Dictionary<int, student>();
            sw.Restart();
            for (int i = 0; i < 10000000; i++)
            {
                lock (dictionaryClass)
                {
                    dictionaryClass[i] = stu;
                }
            }
            sw.Stop();
            Console.WriteLine("wrinting CLASS to dictionaryClass with a lock: {0}", sw.Elapsed.TotalMilliseconds);
            Console.WriteLine("");

            // 字符串写入HashTable 无锁
            sw.Restart();
            for (int i = 0; i < 10000000; i++)
            {
                ht[i] = Item;
            }
            sw.Stop();
            Console.WriteLine("wrinting to HashTable WITHOUT a lock: {0}", sw.Elapsed.TotalMilliseconds);

            //字符串写入HashTable 有锁
            ht = new Hashtable();
            sw.Restart();
            for (int i = 0; i < 10000000; i++)
            {
                lock (ht)
                {
                    ht[i] = Item;
                }
            }
            sw.Stop();
            Console.WriteLine("wrinting to HashTable With a lock: {0}", sw.Elapsed.TotalMilliseconds);

            //类写入HashTable 无锁
            sw.Restart();
            for (int i = 0; i < 10000000; i++)
            {
                htClass[i] = stu;
            }
            sw.Stop();
            Console.WriteLine("wrinting CLASS to HashTableClass WITHOUT a lock: {0}", sw.Elapsed.TotalMilliseconds);

            //类写入HashTable 有锁
            htClass.Clear();
            sw.Restart();
            for (int i = 0; i < 10000000; i++)
            {
                lock (htClass)
                {
                    htClass[i] = stu;
                }
            }
            sw.Stop();
            Console.WriteLine("wrinting CLASS to HashTableClass with a lock: {0}", sw.Elapsed.TotalMilliseconds);
            Console.WriteLine("");



            //字符串写入ConcurrentDictionary
            sw.Restart();
            for (int i = 0; i < 10000000; i++)
            {
                concurrentDictionary[i] = Item;
            }
            sw.Stop();
            Console.WriteLine("wrinting to a concurrent dictionary: {0}", sw.Elapsed.TotalMilliseconds);


            //类写入ConcurrentDictionary
            sw.Restart();
            for (int i = 0; i < 10000000; i++)
            {
                concurrentDictionaryClass[i] = stu;
            }
            sw.Stop();
            Console.WriteLine("wrinting CLASS to a concurrent dictionaryClass: {0}", sw.Elapsed.TotalMilliseconds);
            Console.WriteLine("");
            //对于写入操作并发词典要比普通带锁词典要慢

            //遍历普通字典 有锁
            sw.Restart();
            for (int i = 0; i < 10000000; i++)
            {
                lock (dictionary)
                {
                    CurrentItem = dictionary[i];
                }
            }
            sw.Stop();
            Console.WriteLine("reading from dictionary with a lock: {0}", sw.Elapsed.TotalMilliseconds);

            //遍历普通字典 无锁
            sw.Restart();
            for (int i = 0; i < 10000000; i++)
            {
                CurrentItem = dictionary[i];
            }
            sw.Stop();
            Console.WriteLine("reading from dictionary WITHOUT a lock: {0}", sw.Elapsed.TotalMilliseconds);

            student CurrentStu = new student();

            //遍历普通字典(类) 有锁
            sw.Restart();
            for (int i = 0; i < 10000000; i++)
            {
                lock (dictionaryClass)
                {
                    CurrentStu = dictionaryClass[i];
                }
            }
            sw.Stop();
            Console.WriteLine("reading from dictionaryClass with a lock: {0}", sw.Elapsed.TotalMilliseconds);
            Console.WriteLine("");

            //遍历HashTable 无锁
            sw.Restart();
            for (int i = 0; i < 10000000; i++)
            {
                CurrentItem = ht[i].ToString();
            }
            sw.Stop();
            Console.WriteLine("reading from HastTable WITHOUT a lock: {0}", sw.Elapsed.TotalMilliseconds);

            //遍历HashTable 有锁
            sw.Restart();
            for (int i = 0; i < 10000000; i++)
            {
                lock (ht)
                {
                    CurrentItem = ht[i].ToString();
                }
            }
            sw.Stop();
            Console.WriteLine("reading from HastTable With a lock: {0}", sw.Elapsed.TotalMilliseconds);

            //遍历HashTable(类) 有锁
            sw.Restart();
            for (int i = 0; i < 10000000; i++)
            {
                lock (htClass)
                {
                    CurrentStu = (student)htClass[i];
                }
            }
            sw.Stop();
            Console.WriteLine("reading from HastTableClass with a lock: {0}", sw.Elapsed.TotalMilliseconds);
            Console.WriteLine("");

            //遍历ConCurrent字典
            sw.Restart();
            for (int i = 0; i < 10000000; i++)
            {
                CurrentItem = concurrentDictionary[i];
            }
            sw.Stop();
            Console.WriteLine("reading from a concurrent dictionary: {0}", sw.Elapsed.TotalMilliseconds);

            //遍历ConCurrent字典(类)
            sw.Restart();
            for (int i = 0; i < 10000000; i++)
            {
                CurrentStu = concurrentDictionaryClass[i];
            }
            sw.Stop();
            Console.WriteLine("reading from a concurrent dictionaryClass: {0}", sw.Elapsed.TotalMilliseconds);
            //reading from a concurrent dictionary: 00:00:00.0196372
            //对于读取操作并发词典要比普通带锁词典要快
            //concurrentDictionary采用细粒度锁定[fine-grained locking]
            //普通带锁dictionary采用粗粒度锁定[coarse-grained locking]
            //在多核多线程的情况下concurrentDictionary将有更好的性能表现
            sw.Restart();

            Console.ReadKey();
        }

        const string Item = "Dictionary item";

    }

the above.

Released three original articles · won praise 0 · Views 267

Guess you like

Origin blog.csdn.net/moneymyone/article/details/104796522