15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Performance ❘ 265<br />

O(n) means that for every element in the collection the same amount of additional time is needed.<br />

The Add() method of ArrayList can be an O(n) operation if a reallocation of the collection is required.<br />

Changing the capacity causes the copying of the list, <strong>and</strong> the time for the copy increases linearly with<br />

every element.<br />

O(log n) means that the time needed for the operation increases with every element in the collection. But<br />

the increase of time for every element is not linear but logarithmic. SortedDictionary<br />

has O(log n) behavior for inserting operations inside the collection; SortedList has O(n)<br />

behavior for the same functionality. Here, SortedDictionary is a lot faster because it is<br />

more efficient to insert elements into a tree structure than into a list.<br />

The following table lists collection classes <strong>and</strong> their performance for different actions such as adding, inserting,<br />

<strong>and</strong> removing items. Using this table you can select the best collection class for the purpose of your use.<br />

The left column lists the collection class. The Add column gives timing information about adding items to<br />

the collection. The List <strong>and</strong> the HashSet classes define Add methods to add items to the collection.<br />

With other collection classes, there’s a different method to add elements to the collection; for example, the<br />

Stack class defines a Push() method, <strong>and</strong> the Queue class defines an Enqueue() method. You can<br />

find this information in the table as well.<br />

If there are multiple big-O values in a cell, the reason is that if a collection needs to be resized, resizing takes<br />

a while. For example, with the List class, adding items needs O(1). If the capacity of the collection is<br />

not large enough <strong>and</strong> the collection needs to be resized, the resize requires O(n) time. The larger the collection<br />

is, the longer the resize operation takes. It’s best to avoid resizes by setting the capacity of the collection<br />

to a value that can hold all elements.<br />

If the cell content is na, this means that this operation is not applicable with this collection type.<br />

ColleCTion add tnserT remoVe iTem torT find<br />

List<br />

Stack<br />

Queue<br />

HashSet<br />

SortedSet<br />

LinkedList<br />

O(1) or O(n)<br />

if the collection<br />

must be<br />

resized<br />

Push(), O(1)<br />

or O(n) if the<br />

stack must be<br />

resized<br />

Enqueue(),<br />

O(1) or O(n)<br />

if the queue<br />

must be<br />

resized<br />

O(1) or O(n) if<br />

the set must<br />

be resized<br />

O(1) or O(n) if<br />

the set must<br />

be resized<br />

AddLast()<br />

O(1)<br />

O(n) O(n) O(1) O (n log<br />

n), worst<br />

case O<br />

(n ^ 2)<br />

na Pop(), O(1) na na na<br />

na<br />

Add()<br />

O(1) or<br />

O(n)<br />

Add()<br />

O(1) or<br />

O(n)<br />

Add<br />

After()<br />

O(1)<br />

Dequeue(),<br />

O(1)<br />

O(n)<br />

na na na<br />

O(1) na na na<br />

O(1) na na na<br />

O(1) Na na O(n)<br />

continues<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!