2019-11-29-C # - -TryGetValue- dictionary -Dictionary- -ContainsKey- then determines the first performance comparison -Get-

Original: 2019-11-29-C # - -TryGetValue- dictionary -Dictionary- of the prior determination of the performance comparison -ContainsKey- then -Get-

title author date CreateTime categories
C # Dictionary dictionary of the prior TryGetValue ContainsKey then determines the performance comparison Get
lindexi
2019-11-29 10:13:19 +0800
2018-09-08 15:33:40 +0800
C#

As used herein, benchmarkdotnet performance test dictionary, to obtain the value in the use of a dictionary may be possible to use two different ways of writing, then writing of two performance analysis herein.

Presence determination value, if the value exists on the acquired value, the following two different use methods

One method is to use the code TryGetValue see below

            if (Dictionary.TryGetValue(xx, out var foo))
            {
            }

Another method is to determine whether there is then acquired, consider the following Code

if(Dictionary.ContainsKey(xx))
{
	var foo = Dictionary[xx];
}

So it is used herein benchmarkdotnet performance of the two test methods

The following is the test data, the test code in the final article. TryGetExist here is to try to obtain a value that is there. The ContainGetExist is to determine whether there is value, if there is to try to get this value.

BenchmarkDotNet=v0.10.14, OS=Windows 10.0.17134
Intel Core i7-6700 CPU 3.40GHz (Skylake), 1 CPU, 8 logical and 4 physical cores
Frequency=3328130 Hz, Resolution=300.4690 ns, Timer=TSC
  [Host]     : .NET Framework 4.7 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.7.3132.0  [AttachedDebugger]
  DefaultJob : .NET Framework 4.7 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.7.3132.0

Method Mean Error StdDev Median
TryGetExist 30.26 ns 0.6057 ns 0.5949 ns 30.11 ns
ContainGetExist 46.36 ns 1.0883 ns 3.1919 ns 44.90 ns
TryGetNoExist 20.23 ns 0.4661 ns 0.7658 ns 19.93 ns
ContainGetNoExist 18.68 ns 0.2569 ns 0.2403 ns 18.66 ns

Also compare the performance ConcurrentDictionary thread-safe class, that is, the dictionary will test Foo class above ConcurrentDictionary replaced by other code not modify, the following test data, you can see the performance is still better to use TryGetValue

BenchmarkDotNet=v0.10.14, OS=Windows 10.0.17134
Intel Core i7-6700 CPU 3.40GHz (Skylake), 1 CPU, 8 logical and 4 physical cores
Frequency=3328130 Hz, Resolution=300.4690 ns, Timer=TSC
  [Host]     : .NET Framework 4.7 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.7.3132.0  [AttachedDebugger]
  DefaultJob : .NET Framework 4.7 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.7.3132.0

Method Mean Error StdDev Median
TryGetExist 31.20 ns 0.4644 ns 0.3625 ns 31.17 ns
ContainGetExist 66.80 ns 2.4692 ns 7.2806 ns 63.84 ns
TryGetNoExist 20.07 ns 0.1254 ns 0.1112 ns 20.04 ns
ContainGetNoExist 27.63 ns 0.4230 ns 0.3956 ns 27.65 ns

All codes

    public  class  Foo
    {
        /// <inheritdoc />
        public Foo()
        {
            was  Ran  =  new  Random ();
            bool  seen  =  false ;
            for ( int  in  =  0 ; in  <  100000 ; of ++ )
            {
                LazyDictionary[ran.Next().ToString() + "-" + i.ToString()] = ran.Next().ToString();
                if (!set)
                {
                    if (ran.Next() < i)
                    {
                        set = true;
                        LazyDictionary["lindexi"] = "逗比";
                    }
                }
            }
        }

        [Benchmark]
        public void TryGetExist()
        {
            if (LazyDictionary.TryGetValue("lindexi", out var foo))
            {
                _foo = foo;
            }
        }

        [Benchmark]
        public void ContainGetExist()
        {
            if (LazyDictionary.ContainsKey("lindexi"))
            {
                _foo = LazyDictionary["lindexi"];
            }
        }


        [Benchmark]
        public void TryGetNoExist()
        {
            if (LazyDictionary.TryGetValue("lindexi123", out var foo))
            {
                _foo = foo;
            }
        }

        [Benchmark]
        public void ContainGetNoExist()
        {
            if (LazyDictionary.ContainsKey("lindexi123"))
            {
                _foo = LazyDictionary["lindexi123"];
            }
        }

        private object _foo;

        private Dictionary<string, object> LazyDictionary { get; } = new Dictionary<string, object>();

    }

My blog is about handling synchronized to Tencent cloud + community, inviting all of them settled: https://cloud.tencent.com/developer/support-plan?invite_code=19bm8i8js1ezb

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12075837.html