A faster replacement to the Dictionary

后端 未结 10 853
醉梦人生
醉梦人生 2020-12-08 15:03

I need a fast replacement for the System.Collections.Generic.Dictionary. My application should be really fast. So, the repl

10条回答
  •  一整个雨季
    2020-12-08 15:30

    Don't forget, you're timing the Dictionary constructor in that code as well. I did a test, moving the call to the constructor out of the measurement, and looped 10 times. Here's my test code:

    for (int i = 0; i < 10; i++)
    {
        Dictionary test = new Dictionary();
    
        System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();
    
        test.Add("fieldName", "fieldValue");
        test.Add("Title", "fieldavlkajlkdjflkjalkjslkdjfiajwelkrjelrkjavoijl");
    
        Console.WriteLine(watch.Elapsed);
    }
    
    Console.ReadKey();
    

    Below are the results:

    00:00:00.0000607
    00:00:00.0000025
    00:00:00.0000015
    00:00:00.0000015
    00:00:00.0000016
    00:00:00.0000017
    00:00:00.0000016
    00:00:00.0000016
    00:00:00.0000016
    00:00:00.0000015
    

    I'm not sure how much faster you could get than that...

    Update

    Looks like this mirrors Jon Skeets results too...JIT.

提交回复
热议问题