Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparison of collection datatypes in C# [closed]

Does anyone know of a good overview of the different C# collection types? I am looking for something showing which basic operations such as Add, Remove, RemoveLast etc. are supported, and giving the relative performance.

It would be particularly interesting for the various generic classes - and even better if it showed eg. if there is a difference in performance between a List<T> where T is a class and one where T is a struct.

A start would be a nice cheat-sheet for the abstract data structures, comparing Linked Lists, Hash Tables etc. etc. Thanks!

like image 827
Joel in Gö Avatar asked Jun 15 '09 12:06

Joel in Gö


People also ask

What is a collection of different data types in C?

A structure is a collection of one or more variables, possibly of different types, grouped under a single name. It is a user-defined data type.

Can we compare two data types in C?

If both operands have the same type, then no further conversion is needed. Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.

What are the 5 data types in C?

Most of the time, for small programs, we use the basic fundamental data types in C – int, char, float, and double. For more complex and huge amounts of data, we use derived types – array, structure, union, and pointer. Enumeration and void consist of enum and void, respectively.

What are the 4 data types in C?

The C language provides the four basic arithmetic type specifiers char, int, float and double, and the modifiers signed, unsigned, short, and long. The following table lists the permissible combinations in specifying a large set of storage size-specific declarations.


1 Answers

The following content was originally taken from MSDN http://xbox.create.msdn.com/downloads/?id=123&filename=DataStructures_CheatSheet.doc (but the link has since died).

Complexity table

As in the image above, the content was originally provided as a table (which StackOverflow doesn't support).

Given an image isn't easily indexed below is a somewhat crude programmatic conversion of the information to lists:

Array

  • add to end: O(n)
  • remove from end: O(n)
  • insert at middle: O(n)
  • remove from middle: O(n)
  • Random Access: O(1)
  • In-order Access: O(1)
  • Search for specific element: O(n)
  • Notes: Most efficient use of memory; use in cases where data size is fixed.

List

  • add to end: best case O(1); worst case O(n)
  • remove from end: O(1)
  • insert at middle: O(n)
  • remove from middle: O(n)
  • Random Access: O(1)
  • In-order Access: O(1)
  • Search for specific element: O(n)
  • Notes: Implementation is optimized for speed. In many cases, List will be the best choice.

Collection

  • add to end: best case O(1); worst case O(n)
  • remove from end: O(1)
  • insert at middle: O(n)
  • remove from middle: O(n)
  • Random Access: O(1)
  • In-order Access: O(1)
  • Search for specific element: O(n)
  • Notes: List is a better choice, unless publicly exposed as API.

LinkedList

  • add to end: O(1)
  • remove from end: O(1)
  • insert at middle: O(1)
  • remove from middle: O(1)
  • Random Access: O(n)
  • In-order Access: O(1)
  • Search for specific element: O(n)
  • Notes: Many operations are fast, but watch out for cache coherency.

Stack

  • add to end: best case O(1); worst case O(n)
  • remove from end: O(1)
  • insert at middle: N/A
  • remove from middle: N/A
  • Random Access: N/A
  • In-order Access: N/A
  • Search for specific element: N/A
  • Notes: Shouldn't be selected for performance reasons, but algorithmic ones.

Queue

  • add to end: best case O(1); worst case O(n)
  • remove from end: O(1)
  • insert at middle: N/A
  • remove from middle: N/A
  • Random Access: N/A
  • In-order Access: N/A
  • Search for specific element: N/A
  • Notes: Shouldn't be selected for performance reasons, but algorithmic ones.

Dictionary

  • add to end: best case O(1); worst case O(n)
  • remove from end: O(1)
  • insert at middle: best case O(1); worst case O(n)
  • remove from middle: O(1)
  • Random Access: O(1)*
  • In-order Access: O(1)*
  • Search for specific element: O(1)
  • Notes: Although in-order access time is constant time, it is usually slower than other structures due to the over-head of looking up the key.
like image 146
Benjamin Dobell Avatar answered Sep 30 '22 08:09

Benjamin Dobell