Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Performance comparison of ConcurrentBag vs List

Preface: I'm only asking this because I don't have an environment (dataset large enough + computing power) to test it in a reliable fashion.

Question: Given a Concurrent Bag, loaded with billions of items, being accessed/used by a single thread, does it perform similar to a List? Putting in another words, is the enumeration over a Concurrent Bag any more or less performatic than over a List<T>?

like image 612
Leonardo Avatar asked Feb 15 '16 18:02

Leonardo


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

ConcurrentBag<T> will inevitably be less performant than List<T>. Although you will only be accessing it from a single thread, the structure still needs to have mechanisms in place to protect against the possibility of race hazards should concurrent access arise.

If you will be loading the collection from a single thread before starting your enumerations, you can avoid the performance overhead by using the ConcurrentBag(IEnumerable<T>) constructor, rather than adding each item individually through its Add method.

ConcurrentBag<T> provides “moment-in-time snapshot” semantics for enumerations; see the remarks for its GetEnumerator method. When you access ConcurrentBag<T> from a foreach loop, it will first copy its entire contents into a plain List<T>, then enumerate over that. This will incur a substantial performance overhead (both computation- and memory-wise) each time you use it in a loop.

If your scenario is that your list will be populated by multiple threads, but then only read by one thread, then you should convert it to a List<T> as soon as the writers are done.

like image 157
Douglas Avatar answered Sep 19 '22 00:09

Douglas