SlideShare una empresa de Scribd logo
1 de 36
Collection Classes Deep Dive

        By Gary Short
     Head of Gibraltar Labs
      Gibraltar Software


                               1
Introduction
•   Gary Short
•   Head of Gibraltar Labs
•   C# MVP
•   @garyshort
•   gary.short@gibraltarsoftare.com
•   facebook.com/TheOtherGaryShort



                                      2
Why do we Care About This Stuff?




                                   3
4
Let’s Start With Something We Know




                                     5
List<T> Demo




               6
What we Learned
•   Don’t add elements in a loop
•   Add causes capacity growths
•   Capacity growths uses Array.Copy()
•   Array.Copy() is a O(n) operation
•   O(n) is sloooooowwwwwww. 
•   Use AddRange() instead
•   Or set “large enough” initial capacity.

                                              7
How Slow is Slow?




                    8
Performance: Add Versus AddRange
                  30000


                  25000


                  20000
Number of Ticks




                  15000
                                                                               Add
                                                                               AddRange
                  10000


                  5000


                      0
                          10   100       1000      10000    100000   1000000
                                     Number of Elements Added
What About Removing Stuff?




                             10
Demo




       11
What we Learned



Prefer RemoveAt() as there’s no IndexOf() step




                                                 12
List<T> - Sorting
•   Uses QuickSort under the hood
•   Fastest general purpose sort algorithm
•   O(n log n) in best case
•   O(n log n) in average case
•   Though worst case is O(n^2) 




                                             13
Performance: O(n log n) Vs O(n^2)
         120


         100


          80
Effort




          60
                                                                   O(n log n)
                                                                   O(n^2)
          40


          20


           0
               1   2      3   4     5      6      7   8   9   10
                              Elements to be Sorted
QuickSort Demo




                 15
So What is the Worst Case?

• If the list is already sorted
   – First partition has lower = 0, upper = n
   – Then calls Partition(n-1);
   – This happens a further n-2 times




                                                16
Can we Mitigate the Worst Case?
• Median of Three
  – Take an element from the “top” of the array
  – Take an element from the “middle” of the array
  – Take an element from the “bottom” of the array
  – Find the median value of the three
  – Pivot on the median
• Let’s see if Microsoft uses this algorithm.


                                                     18
Disadvantage: O(n) Add, Insert, Remove




                                         19
What if we Need Fast Add, Insert & Remove?




                                             20
LinkedList<T>
• Double linked
  – Each item points to the previous and next items
  – This means it’s super fast
     • Add, insert and remove are all O(1) operations




                                                        21
Demo




       22
Disadvantage: O(n) lookups




                             23
What if we Need Fast Lookups?




                                24
Dictionary<TKey, TValue>
• Performance depends on key.GetHashCode()
  – Hash codes must be evenly distributed across int
     • If two keys return hashes that give the same index
        – Dictionary must look for nearest free location to store item
        – Must search later to return the item
        – This hurts performance
  – Use your own type, then this is on you. 




                                                                         25
Dictionary<TKey, TValue>
• Objects used as keys must also implement
  IEquatable.Equals()
• Or override Equals()
• Why?
  – Different keys may return the same hashcode
  – Equals() is used by the dictionary comparing keys
  – So you must ensure the following
     • If A.Equals(B) then A.HashCode() and B.HashCode() return
       the same HashCode()
     • Override Equals() but not GetHashCode() == compile error.


                                                                   26
Disadvantage: one value per key




                                  27
What if I Need Multiple Values per Key?




                                          28
Lookup<TKey, TElement> Demo




                              29
Concurrent Collections




                         30
Types of Concurrent Collections
•   ConcurrentBag<T>
•   ConcurrentDictionary<T>
•   ConcurrentQueue<T>
•   ConcurrentStack<T>
•   OrderablePartitioner<T>
•   BlockingCollection<T>.



                                      31
Key Characteristics
• New .Net 4.0
• Guards against multi-thread collection conflicts
• Implements IProducerConsumerCollections<T>
   – TryAdd()
      • Tries to add item to collection returns success bool
   – TryTake()
      • Tries to remove and return item returns success bool
          – Returns the item in an out param.

• Always check the return value before moving on.

                                                               32
Do I Have To Check Every Time?!
• BlockingCollection<T>
  – Blocks and waits until task completes
  – Uses Add() and Take() methods
     • Block the thread and wait until task completes
     • Add() has an overload to pass a CancellationToken
     • Add() may also block if bounding capacity was used.




                                                             33
But I Don’t Want it to Wait For Ever!
• So we don’t want to wait forever
• Nor do we want to cancel the Add() from
  outside
• TryAdd() and TryTake() are offered too
• Where you can specify a timeout.




                                            34
Summary
• List is a good general purpose collection
    – Construct to size if possible
    – Construct to upper threshold then trim
    – Prefer AddRange() over Add()
    – Be aware of “Quicksort Killers”
•   Use LinkedList if you need fast insert/remove
•   Use Dictionary if you need fast lookup
•   Use Lookup if you need multi values
•   Use concurrent collections for thread safety.   35
Questions
• gary.short@gibraltarsoftware.com
• @garyshort
• Facebook.com/TheOtherGaryShort




                                     36

Más contenido relacionado

La actualidad más candente

Understanding Javascript Engines
Understanding Javascript Engines Understanding Javascript Engines
Understanding Javascript Engines Parashuram N
 
Educational slides by venay magen
Educational slides by venay magenEducational slides by venay magen
Educational slides by venay magenvenaymagen19
 
The Ring programming language version 1.2 book - Part 24 of 84
The Ring programming language version 1.2 book - Part 24 of 84The Ring programming language version 1.2 book - Part 24 of 84
The Ring programming language version 1.2 book - Part 24 of 84Mahmoud Samir Fayed
 
JavaScript Foundations Day1
JavaScript Foundations Day1JavaScript Foundations Day1
JavaScript Foundations Day1Troy Miles
 
The Ring programming language version 1.6 book - Part 32 of 189
The Ring programming language version 1.6 book - Part 32 of 189The Ring programming language version 1.6 book - Part 32 of 189
The Ring programming language version 1.6 book - Part 32 of 189Mahmoud Samir Fayed
 
Scala-对Java的修正和超越
Scala-对Java的修正和超越Scala-对Java的修正和超越
Scala-对Java的修正和超越Caoyuan Deng
 
Introduction to idris
Introduction to idrisIntroduction to idris
Introduction to idrisConor Farrell
 
From Lisp to Clojure/Incanter and RAn Introduction
From Lisp to Clojure/Incanter and RAn IntroductionFrom Lisp to Clojure/Incanter and RAn Introduction
From Lisp to Clojure/Incanter and RAn Introductionelliando dias
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimizationg3_nittala
 
The Ring programming language version 1.2 book - Part 22 of 84
The Ring programming language version 1.2 book - Part 22 of 84The Ring programming language version 1.2 book - Part 22 of 84
The Ring programming language version 1.2 book - Part 22 of 84Mahmoud Samir Fayed
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scalaXing
 
The Ring programming language version 1.10 book - Part 39 of 212
The Ring programming language version 1.10 book - Part 39 of 212The Ring programming language version 1.10 book - Part 39 of 212
The Ring programming language version 1.10 book - Part 39 of 212Mahmoud Samir Fayed
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesTomer Gabel
 
Functional Programming Past Present Future
Functional Programming Past Present FutureFunctional Programming Past Present Future
Functional Programming Past Present FutureIndicThreads
 
How to Think in RxJava Before Reacting
How to Think in RxJava Before ReactingHow to Think in RxJava Before Reacting
How to Think in RxJava Before ReactingIndicThreads
 
The Ring programming language version 1.4 book - Part 9 of 30
The Ring programming language version 1.4 book - Part 9 of 30The Ring programming language version 1.4 book - Part 9 of 30
The Ring programming language version 1.4 book - Part 9 of 30Mahmoud Samir Fayed
 

La actualidad más candente (20)

Understanding Javascript Engines
Understanding Javascript Engines Understanding Javascript Engines
Understanding Javascript Engines
 
Educational slides by venay magen
Educational slides by venay magenEducational slides by venay magen
Educational slides by venay magen
 
Unit – iv queue
Unit – iv    queueUnit – iv    queue
Unit – iv queue
 
The Ring programming language version 1.2 book - Part 24 of 84
The Ring programming language version 1.2 book - Part 24 of 84The Ring programming language version 1.2 book - Part 24 of 84
The Ring programming language version 1.2 book - Part 24 of 84
 
Knolx session
Knolx sessionKnolx session
Knolx session
 
JavaScript Foundations Day1
JavaScript Foundations Day1JavaScript Foundations Day1
JavaScript Foundations Day1
 
The Ring programming language version 1.6 book - Part 32 of 189
The Ring programming language version 1.6 book - Part 32 of 189The Ring programming language version 1.6 book - Part 32 of 189
The Ring programming language version 1.6 book - Part 32 of 189
 
Scala-对Java的修正和超越
Scala-对Java的修正和超越Scala-对Java的修正和超越
Scala-对Java的修正和超越
 
Introduction to idris
Introduction to idrisIntroduction to idris
Introduction to idris
 
From Lisp to Clojure/Incanter and RAn Introduction
From Lisp to Clojure/Incanter and RAn IntroductionFrom Lisp to Clojure/Incanter and RAn Introduction
From Lisp to Clojure/Incanter and RAn Introduction
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimization
 
The Ring programming language version 1.2 book - Part 22 of 84
The Ring programming language version 1.2 book - Part 22 of 84The Ring programming language version 1.2 book - Part 22 of 84
The Ring programming language version 1.2 book - Part 22 of 84
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scala
 
The Ring programming language version 1.10 book - Part 39 of 212
The Ring programming language version 1.10 book - Part 39 of 212The Ring programming language version 1.10 book - Part 39 of 212
The Ring programming language version 1.10 book - Part 39 of 212
 
Algorithm and Programming (Array)
Algorithm and Programming (Array)Algorithm and Programming (Array)
Algorithm and Programming (Array)
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
 
Αλγόριθμοι
Αλγόριθμοι Αλγόριθμοι
Αλγόριθμοι
 
Functional Programming Past Present Future
Functional Programming Past Present FutureFunctional Programming Past Present Future
Functional Programming Past Present Future
 
How to Think in RxJava Before Reacting
How to Think in RxJava Before ReactingHow to Think in RxJava Before Reacting
How to Think in RxJava Before Reacting
 
The Ring programming language version 1.4 book - Part 9 of 30
The Ring programming language version 1.4 book - Part 9 of 30The Ring programming language version 1.4 book - Part 9 of 30
The Ring programming language version 1.4 book - Part 9 of 30
 

Similar a .Net Collection Classes Deep Dive - Rocksolid Tour 2013

An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...Tosin Amuda
 
0-Slot18-19-20-ContiguousStorage.pdf
0-Slot18-19-20-ContiguousStorage.pdf0-Slot18-19-20-ContiguousStorage.pdf
0-Slot18-19-20-ContiguousStorage.pdfssusere19c741
 
JDK8 Functional API
JDK8 Functional APIJDK8 Functional API
JDK8 Functional APIJustin Lin
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for publiciqbalphy1
 
Week2-stacks-queues.pptx
Week2-stacks-queues.pptxWeek2-stacks-queues.pptx
Week2-stacks-queues.pptxVandanaBharti21
 
Exploiting Concurrency with Dynamic Languages
Exploiting Concurrency with Dynamic LanguagesExploiting Concurrency with Dynamic Languages
Exploiting Concurrency with Dynamic LanguagesTobias Lindaaker
 
.NET Fest 2018. Дмитрий Иванов. Иммутабельные структуры данных в .NET: зачем ...
.NET Fest 2018. Дмитрий Иванов. Иммутабельные структуры данных в .NET: зачем ....NET Fest 2018. Дмитрий Иванов. Иммутабельные структуры данных в .NET: зачем ...
.NET Fest 2018. Дмитрий Иванов. Иммутабельные структуры данных в .NET: зачем ...NETFest
 
stacks and queues class 12 in c++
stacks and  queues class 12 in c++stacks and  queues class 12 in c++
stacks and queues class 12 in c++Khushal Mehta
 
Yin Yangs of Software Development
Yin Yangs of Software DevelopmentYin Yangs of Software Development
Yin Yangs of Software DevelopmentNaveenkumar Muguda
 
2. overview of c#
2. overview of c#2. overview of c#
2. overview of c#Rohit Rao
 
Guava Overview. Part 1 @ Bucharest JUG #1
Guava Overview. Part 1 @ Bucharest JUG #1 Guava Overview. Part 1 @ Bucharest JUG #1
Guava Overview. Part 1 @ Bucharest JUG #1 Andrei Savu
 
lecture-k-sorting.ppt
lecture-k-sorting.pptlecture-k-sorting.ppt
lecture-k-sorting.pptSushantRaj25
 
Stack squeues lists
Stack squeues listsStack squeues lists
Stack squeues listsJames Wong
 
Stacksqueueslists
StacksqueueslistsStacksqueueslists
StacksqueueslistsFraboni Ec
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues listsYoung Alista
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues listsTony Nguyen
 

Similar a .Net Collection Classes Deep Dive - Rocksolid Tour 2013 (20)

An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
 
0-Slot18-19-20-ContiguousStorage.pdf
0-Slot18-19-20-ContiguousStorage.pdf0-Slot18-19-20-ContiguousStorage.pdf
0-Slot18-19-20-ContiguousStorage.pdf
 
JDK8 Functional API
JDK8 Functional APIJDK8 Functional API
JDK8 Functional API
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
Week2-stacks-queues.pptx
Week2-stacks-queues.pptxWeek2-stacks-queues.pptx
Week2-stacks-queues.pptx
 
Taming Asynchrony using RxJS
Taming Asynchrony using RxJSTaming Asynchrony using RxJS
Taming Asynchrony using RxJS
 
Exploiting Concurrency with Dynamic Languages
Exploiting Concurrency with Dynamic LanguagesExploiting Concurrency with Dynamic Languages
Exploiting Concurrency with Dynamic Languages
 
.NET Fest 2018. Дмитрий Иванов. Иммутабельные структуры данных в .NET: зачем ...
.NET Fest 2018. Дмитрий Иванов. Иммутабельные структуры данных в .NET: зачем ....NET Fest 2018. Дмитрий Иванов. Иммутабельные структуры данных в .NET: зачем ...
.NET Fest 2018. Дмитрий Иванов. Иммутабельные структуры данных в .NET: зачем ...
 
stacks and queues class 12 in c++
stacks and  queues class 12 in c++stacks and  queues class 12 in c++
stacks and queues class 12 in c++
 
Yin Yangs of Software Development
Yin Yangs of Software DevelopmentYin Yangs of Software Development
Yin Yangs of Software Development
 
2. overview of c#
2. overview of c#2. overview of c#
2. overview of c#
 
Guava Overview. Part 1 @ Bucharest JUG #1
Guava Overview. Part 1 @ Bucharest JUG #1 Guava Overview. Part 1 @ Bucharest JUG #1
Guava Overview. Part 1 @ Bucharest JUG #1
 
lecture-k-sorting.ppt
lecture-k-sorting.pptlecture-k-sorting.ppt
lecture-k-sorting.ppt
 
3.ppt
3.ppt3.ppt
3.ppt
 
Lecture k-sorting
Lecture k-sortingLecture k-sorting
Lecture k-sorting
 
Stack squeues lists
Stack squeues listsStack squeues lists
Stack squeues lists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Stacksqueueslists
StacksqueueslistsStacksqueueslists
Stacksqueueslists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 

Más de Gary Short

Raspberry Pi - Rocksolid Tour 2013
Raspberry Pi  - Rocksolid Tour 2013Raspberry Pi  - Rocksolid Tour 2013
Raspberry Pi - Rocksolid Tour 2013Gary Short
 
Not Everything is an Object - Rocksolid Tour 2013
Not Everything is an Object  - Rocksolid Tour 2013Not Everything is an Object  - Rocksolid Tour 2013
Not Everything is an Object - Rocksolid Tour 2013Gary Short
 
Marginal Gains - Rocksolid Tour 2013
Marginal Gains  - Rocksolid Tour 2013Marginal Gains  - Rocksolid Tour 2013
Marginal Gains - Rocksolid Tour 2013Gary Short
 
Algorithms - Rocksolid Tour 2013
Algorithms  - Rocksolid Tour 2013Algorithms  - Rocksolid Tour 2013
Algorithms - Rocksolid Tour 2013Gary Short
 
Building an Internet Radio on the RaspberryPI
Building an Internet Radio on the RaspberryPIBuilding an Internet Radio on the RaspberryPI
Building an Internet Radio on the RaspberryPIGary Short
 
Connecting to the Raspberry Pi from your Laptop
Connecting to the Raspberry Pi from your LaptopConnecting to the Raspberry Pi from your Laptop
Connecting to the Raspberry Pi from your LaptopGary Short
 
Setting up the Raspberry Pi Using BerryBoot
Setting up the Raspberry Pi Using BerryBootSetting up the Raspberry Pi Using BerryBoot
Setting up the Raspberry Pi Using BerryBootGary Short
 
Everything you Wanted to Know About Refactoring
Everything you Wanted to Know About RefactoringEverything you Wanted to Know About Refactoring
Everything you Wanted to Know About RefactoringGary Short
 
Not Everything Is An Object
Not Everything Is An ObjectNot Everything Is An Object
Not Everything Is An ObjectGary Short
 
Technical Debt
Technical DebtTechnical Debt
Technical DebtGary Short
 

Más de Gary Short (10)

Raspberry Pi - Rocksolid Tour 2013
Raspberry Pi  - Rocksolid Tour 2013Raspberry Pi  - Rocksolid Tour 2013
Raspberry Pi - Rocksolid Tour 2013
 
Not Everything is an Object - Rocksolid Tour 2013
Not Everything is an Object  - Rocksolid Tour 2013Not Everything is an Object  - Rocksolid Tour 2013
Not Everything is an Object - Rocksolid Tour 2013
 
Marginal Gains - Rocksolid Tour 2013
Marginal Gains  - Rocksolid Tour 2013Marginal Gains  - Rocksolid Tour 2013
Marginal Gains - Rocksolid Tour 2013
 
Algorithms - Rocksolid Tour 2013
Algorithms  - Rocksolid Tour 2013Algorithms  - Rocksolid Tour 2013
Algorithms - Rocksolid Tour 2013
 
Building an Internet Radio on the RaspberryPI
Building an Internet Radio on the RaspberryPIBuilding an Internet Radio on the RaspberryPI
Building an Internet Radio on the RaspberryPI
 
Connecting to the Raspberry Pi from your Laptop
Connecting to the Raspberry Pi from your LaptopConnecting to the Raspberry Pi from your Laptop
Connecting to the Raspberry Pi from your Laptop
 
Setting up the Raspberry Pi Using BerryBoot
Setting up the Raspberry Pi Using BerryBootSetting up the Raspberry Pi Using BerryBoot
Setting up the Raspberry Pi Using BerryBoot
 
Everything you Wanted to Know About Refactoring
Everything you Wanted to Know About RefactoringEverything you Wanted to Know About Refactoring
Everything you Wanted to Know About Refactoring
 
Not Everything Is An Object
Not Everything Is An ObjectNot Everything Is An Object
Not Everything Is An Object
 
Technical Debt
Technical DebtTechnical Debt
Technical Debt
 

.Net Collection Classes Deep Dive - Rocksolid Tour 2013

  • 1. Collection Classes Deep Dive By Gary Short Head of Gibraltar Labs Gibraltar Software 1
  • 2. Introduction • Gary Short • Head of Gibraltar Labs • C# MVP • @garyshort • gary.short@gibraltarsoftare.com • facebook.com/TheOtherGaryShort 2
  • 3. Why do we Care About This Stuff? 3
  • 4. 4
  • 5. Let’s Start With Something We Know 5
  • 7. What we Learned • Don’t add elements in a loop • Add causes capacity growths • Capacity growths uses Array.Copy() • Array.Copy() is a O(n) operation • O(n) is sloooooowwwwwww.  • Use AddRange() instead • Or set “large enough” initial capacity. 7
  • 8. How Slow is Slow? 8
  • 9. Performance: Add Versus AddRange 30000 25000 20000 Number of Ticks 15000 Add AddRange 10000 5000 0 10 100 1000 10000 100000 1000000 Number of Elements Added
  • 10. What About Removing Stuff? 10
  • 11. Demo 11
  • 12. What we Learned Prefer RemoveAt() as there’s no IndexOf() step 12
  • 13. List<T> - Sorting • Uses QuickSort under the hood • Fastest general purpose sort algorithm • O(n log n) in best case • O(n log n) in average case • Though worst case is O(n^2)  13
  • 14. Performance: O(n log n) Vs O(n^2) 120 100 80 Effort 60 O(n log n) O(n^2) 40 20 0 1 2 3 4 5 6 7 8 9 10 Elements to be Sorted
  • 16. So What is the Worst Case? • If the list is already sorted – First partition has lower = 0, upper = n – Then calls Partition(n-1); – This happens a further n-2 times 16
  • 17.
  • 18. Can we Mitigate the Worst Case? • Median of Three – Take an element from the “top” of the array – Take an element from the “middle” of the array – Take an element from the “bottom” of the array – Find the median value of the three – Pivot on the median • Let’s see if Microsoft uses this algorithm. 18
  • 19. Disadvantage: O(n) Add, Insert, Remove 19
  • 20. What if we Need Fast Add, Insert & Remove? 20
  • 21. LinkedList<T> • Double linked – Each item points to the previous and next items – This means it’s super fast • Add, insert and remove are all O(1) operations 21
  • 22. Demo 22
  • 24. What if we Need Fast Lookups? 24
  • 25. Dictionary<TKey, TValue> • Performance depends on key.GetHashCode() – Hash codes must be evenly distributed across int • If two keys return hashes that give the same index – Dictionary must look for nearest free location to store item – Must search later to return the item – This hurts performance – Use your own type, then this is on you.  25
  • 26. Dictionary<TKey, TValue> • Objects used as keys must also implement IEquatable.Equals() • Or override Equals() • Why? – Different keys may return the same hashcode – Equals() is used by the dictionary comparing keys – So you must ensure the following • If A.Equals(B) then A.HashCode() and B.HashCode() return the same HashCode() • Override Equals() but not GetHashCode() == compile error. 26
  • 28. What if I Need Multiple Values per Key? 28
  • 31. Types of Concurrent Collections • ConcurrentBag<T> • ConcurrentDictionary<T> • ConcurrentQueue<T> • ConcurrentStack<T> • OrderablePartitioner<T> • BlockingCollection<T>. 31
  • 32. Key Characteristics • New .Net 4.0 • Guards against multi-thread collection conflicts • Implements IProducerConsumerCollections<T> – TryAdd() • Tries to add item to collection returns success bool – TryTake() • Tries to remove and return item returns success bool – Returns the item in an out param. • Always check the return value before moving on. 32
  • 33. Do I Have To Check Every Time?! • BlockingCollection<T> – Blocks and waits until task completes – Uses Add() and Take() methods • Block the thread and wait until task completes • Add() has an overload to pass a CancellationToken • Add() may also block if bounding capacity was used. 33
  • 34. But I Don’t Want it to Wait For Ever! • So we don’t want to wait forever • Nor do we want to cancel the Add() from outside • TryAdd() and TryTake() are offered too • Where you can specify a timeout. 34
  • 35. Summary • List is a good general purpose collection – Construct to size if possible – Construct to upper threshold then trim – Prefer AddRange() over Add() – Be aware of “Quicksort Killers” • Use LinkedList if you need fast insert/remove • Use Dictionary if you need fast lookup • Use Lookup if you need multi values • Use concurrent collections for thread safety. 35

Notas del editor

  1. Why should we care?It’s all about performance.Performance is the most important thing… apart from everything elsePerformance is like currency, the more you have, the more stuff you can buy.