Performance Tuning of .NET Application
-
Upload
mainul-islam -
Category
Documents
-
view
35 -
download
2
Transcript of Performance Tuning of .NET Application
✓ What is Performance Tuning?
✓ How to profile performance?
✓ Identify hotspot bottlenecks
✓ Ways to optimize .NET Application.
Why Are We Here?
Lets Explore
“Performance tuning is the improvement of system performance”
Your app
Reality of Performance
You
Where is the Bottleneck?for (int n = 2; n < 10000; ++n)
{
bool prime = true;
for (int d = 2; d < n; ++d)
{
if (n % d == 0)
{
prime = false;
break;
}
}
if (prime) Console.WriteLine(n);
}
“Premature optimization is the root of all evil”
- Donald Knuth
Mistakes of Performance Tuning✓ Optimize without a reliable measurement
✓ Optimize without verifying the results
✓ Relying on gut feeling
Performance Matrices
CPUExecution time,
queuing
Memory Utilization, load %
DiskUtilization, I/O Operation
NetworkBandwidth, queueing
How does it work?1. Select Application to Profile
2. Start Profiling3. Collect Data and Detect Bottlenecks4. Optimize Performance5. Compare Profiling Results6. Repeat Until Desired Results (Or
Possible)
dotTrace+
dotMemory
Choosing a Collection
Dozens of Builtin Collections✓ List<T>✓ ArrayList<T>✓ Dictionary<K,V>✓ HashSet<T>✓ Queue<T>✓ SortedList<K,V>✓ SortedDictionary<K,V>✓ SortedSet<T>✓ Stack<T>… and more
Choose Proper Collection ■ Stop using List<T> for everything■ Ask yourself:
Do you need to access each element by index?For zero-based index: Use ArrayList and StringCollection classes and
the List<T> generic class.
For key based index: The Hashtable, SortedList, ListDictionary, and
StringDictionary classes, and the Dictionary<TKey, TValue> and
SortedDictionary<TKey, TValue> generic classes.
Do you need to sort your collection?
The Hashtable class sorts its elements by their hash codes.
The SortedDictionary<TKey, TValue> and SortedList<TKey, TValue> generic
classes sort their elements by the key.
Collection - (Contd.)
Do you need to search your collection?ListDictionary is faster than Hashtable for small collections
(10 items or fewer).
The Dictionary<TKey, TValue> generic class provides faster
lookup than the SortedDictionary<TKey, TValue> generic class.
The multi-threaded implementation is
ConcurrentDictionary<TKey, TValue>. ConcurrentBag<T>
provides fast multi-threaded insertion for unordered data.
Do you need collections that accept only strings?
StringCollection (based on IList) and StringDictionary
(based on IDictionary).
Collection Ordering Direct Access?
Lookup Efficiency
Manipulate Efficiency Notes
Dictionary Unordered Via Key Key: O(1) O(1) Best for high performance lookups.
SortedDictionary Sorted Via Key Key: O(log n) O(log n) Compromise of Dictionary speed and ordering, uses binary search tree.
List Unordered Via Index Index: O(1)Value: O(n) O(n) Best for smaller lists where direct
access required and no sorting.
SortedList Sorted Via Key Key: O(log n) O(n) Faster lookup on preloaded data, but slower loads.
LinkedList Unordered No Key: O(n) O(1)Best for lists where inserting/deleting in middle is common and no direct access
required.
HashSet Unordered Via Key Key: O(1) O(1)Unique unordered collection, like a
Dictionary except key and value are same object.
Make your Application as Parallel as Necessary, But not More
Kind of Parallelism
Parallelism✓ Running multiple threads in parallel✓ .NET 4.0 provide “Task Parallel Library(TPL)” to
simplify parallelism✓ But, Threads are expensive - context switch, lock
Asynchrony✓ Without blocking the caller’s thread✓ .NET 4.5 introduce async programming
Demo
Manage Memory Efficiently
GC as a Performance Problem■ The .NET GC is non-deterministic
✓ Contributes unexpecteds, potentially very long delays that destroy responsiveness.
✓ No way to anticipate the delay or react to it.
There’s no GC if you don’t allocate. Reducing allocation is key to
reducing GC costs.
Reducing Allocation✓ Use value types✓ Pool large objects (such as buffers)✓ Avoid allocation large temporary objects
Use IDisposable
✓ Finalize is expensive.✓ Dispose the object that is not needed.✓ GC does not call Dispose() for you. It’s
your duty to call it.
Demo
Going Further✓ Making .NET Application Faster✓ Making .NET Application Even Faster✓ IDisposable Best Practices for C# Developers✓ Entity Framework Database Performance Anti-Pattern✓ Measuring .NET Performance✓ .NET Performance Optimization and Profiling with JetBrains
dotTrace✓ http://geekswithblogs.
net/BlackRabbitCoder/archive/2011/06/16/c.net-fundamentals-choosing-the-right-collection-class.aspx
✓ https://msdn.microsoft.com/en-us/library/dd460717(v=vs.100).aspx
THANKSAny questions?You can find me at
✓ @mainul098✓ [email protected]