Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConcurrentDictionary<> performance at a single thread misunderstanding?

Tags:

Related brief info:

AFAIK , The concurrent stack, queue, and bag classes are implemented internally with linked lists.
And I know that there is much less contention because each thread is responsible for its own linked list. Any way , my question is about the ConcurrentDictionary<,>

But I was testing this code :(single thread)

Stopwatch sw = new Stopwatch(); sw.Start();      var d = new ConcurrentDictionary < int,  int > ();     for(int i = 0; i < 1000000; i++) d[i] = 123;     for(int i = 1000000; i < 2000000; i++) d[i] = 123;     for(int i = 2000000; i < 3000000; i++) d[i] = 123;     Console.WriteLine("baseline = " + sw.Elapsed);  sw.Restart();      var d2 = new Dictionary < int, int > ();     for(int i = 0; i < 1000000; i++)         lock (d2) d2[i] = 123;     for(int i = 1000000; i < 2000000; i++)   lock (d2) d2[i] = 123;     for(int i = 2000000; i < 3000000; i++)   lock (d2) d2[i] = 123;     Console.WriteLine("baseline = " + sw.Elapsed);  sw.Stop(); 

Result : (tested many times, same values (+/-)).

baseline = 00:00:01.2604656 baseline = 00:00:00.3229741 

Question :

What makes ConcurrentDictionary<,> much slower in a single threaded environment ?

My first instinct is that lock(){} will be always slower. but apparently it is not.

like image 821
Royi Namir Avatar asked Mar 06 '13 15:03

Royi Namir


People also ask

Is ConcurrentDictionary slower?

In scenarios that involve many reads and many updates, the ConcurrentDictionary generally is significantly faster on computers that have any number of cores.

Is ConcurrentDictionary keys thread-safe?

Yes, its threadsafe.

Is ConcurrentDictionary clear thread-safe?

Concurrent. ConcurrentDictionary<TKey,TValue>. This collection class is a thread-safe implementation.

What is ConcurrentDictionary?

ConcurrentDictionary is a generic collection, ConcurrentDictionary was introduced in . NET framework 4.0 as it is available in System. Collections. Concurrent namespace, this generic collection is used in the case of a multi-threaded application.


Video Answer


1 Answers

Well, ConcurrentDictionary is allowing for the possibility that it can be used by multiple threads. It seems entirely reasonable to me that that requires more internal housekeeping than something which assumes it can get away without worrying about access from multiple threads. I'd have been very surprised if it had worked out the other way round - if the safer version were always faster too, why would you ever use the less safe version?

like image 187
Jon Skeet Avatar answered Sep 21 '22 14:09

Jon Skeet