improving performance in .net applications
TRANSCRIPT
Improving Performance in .NET Applications
Jason BockPractice Lead
»http://www.magenic.com
»http://www.jasonbock.net
»https://www.twitter.com/jasonbock
»https://www.github.com/jasonbock
Personal Info
https://github.com/JasonBock/
ImprovingPerformance
http://www.slideshare.net/jasonbock/improving-performance-in-net-applications-41569122
Downloads
»Performance
»Techniques, Tools and Tips
Overview
Remember…
https://github.com/JasonBock/
ImprovingPerformance
http://www.slideshare.net/jasonbock/improving-performance-in-net-applications-41569122
Performance
http://citeseerx.ist.psu.edu/viewdoc/download;jsessionid=2B7D23F17D6379CCABDACC32C0E02140?doi=10.1.1.
103.6084&rep=rep1&type=pdf
http://muhammedozdemir.files.wordpress.com/2010/11/donald_knuth.jpg
“Premature optimization is the root of all evil” – Donald
Knuth
Performance
http://joeduffyblog.com/2010/09/06/the-premature-optimization-is-evil-myth/
“Mostly this quip is used defend sloppy decision-making, or to justify the indefinite deferral of decision-making. In other words, laziness. It
is safe to say that the very mention of this oft-misquoted phrase causes an immediate visceral reaction to commence within me… and
it’s not a pleasant one.” – Joe Duffy
“A phrase like ‘premature optimization is the root of all evil’ is not a get out of jail card for being lazy.” – Richard Brantley
Performance
http://1.bp.blogspot.com/_EUzvVB2uRZI/TM5LHdryAoI/AAAAAAAAAJM/Dyajh-
EwctY/s1600/computer+on+fire.png
Performance
http://stackexchange.com/performance
…
Performance
http://www.troyhunt.com/2014/12/applied-azure-infographic-of-how-have-i.html
Performance
http://lifehackery.com/qimages/6/pentiumee_processor_back.jpg
http://www.ikea.com/us/en/images/products/sy-sewing-thread__63544_PE171202_S4.JPG
http://www.gup20.com/images/folders.jpg
Performance
UI
Service
Business Logic
Data Store
Core
Message Bus
CLR
»Memory management
»Garbage collection
»Code loading and execution
»Structured exception handling
»Common type system
»Security
Performance
Performance
http://www.clker.com/cliparts/I/b/r/1/6/n/simple-green-check-button-hi.png
Techniques, Tools and Tips
Techniques, Tools and Tips
http://www.stellman-greene.com/blog/wp-content/uploads/2008/09/sally-code-review.png
Techniques, Tools and Tips
http://en.wikipedia.org/wiki/Sch%C3%B6nhage%E2%80%93Strassen_algorithm
346* 27
422821
1286
439 2
Grade school: O(n2)
Schönhage–Strassen:O(n log n log log n)
Techniques, Tools and Tips
Techniques, Tools and Tips
http://groomsadvice.com/wp-content/uploads/2011/05/bigstock_Tape_Measure_14084828.jpg
Techniques, Tools and Tips
http://cdn2.whatchristianswanttoknow.com/wp-content/uploads/2014/01/10-Convicting-Questions-To-Ask-Yourself-
Today.jpg
Techniques, Tools and Tipsprivate static BigInteger MultiplyViaOperation(BigInteger a, BigInteger b){return a * b;
}
private static BigInteger MultiplyViaAddition(BigInteger a, BigInteger b){var result = BigInteger.Zero;
var value = BigInteger.Abs(a);for (var i = BigInteger.Zero; i < BigInteger.Abs(b); i++){result += value;
}
return a.Sign < 0 ^ b.Sign < 0 ? BigInteger.Negate(result) : result;}
Techniques, Tools and Tipsprivate static void NaiveTimeTest(BigInteger a, BigInteger b){var operationTime = Stopwatch.StartNew();Program.MultiplyViaOperation(a, b);operationTime.Stop();
Console.Out.WriteLine("NaiveTestTime.MultiplyViaOperation({0}, {1}) time: {2}",a, b, operationTime.Elapsed);
var additionTime = Stopwatch.StartNew();Program.MultiplyViaAddition(a, b);additionTime.Stop();
Console.Out.WriteLine("NaiveTestTime.MultiplyViaAddition({0}, {1}) time: {2}",a, b, additionTime.Elapsed);
}
Techniques, Tools and Tips
Techniques, Tools and Tips
NaiveTestTime.MultiplyViaOperation(3, 2) time: 00:00:00.0001387NaiveTestTime.MultiplyViaAddition(3, 2) time: 00:00:00.0004056NaiveTestTime.MultiplyViaOperation(30, 20) time: 00:00:00.0000931NaiveTestTime.MultiplyViaAddition(30, 20) time: 00:00:00.0002379NaiveTestTime.MultiplyViaOperation(3000, 2000) time: 00:00:00.0001246NaiveTestTime.MultiplyViaAddition(3000, 2000) time: 00:00:00.0005413NaiveTestTime.MultiplyViaOperation(300000, 200000) time: 00:00:00.0000897NaiveTestTime.MultiplyViaAddition(300000, 200000) time: 00:00:00.0461862NaiveTestTime.MultiplyViaOperation(30000000, 20000000) time: 00:00:00.0000885NaiveTestTime.MultiplyViaAddition(30000000, 20000000) time: 00:00:04.6905107
Techniques, Tools and Tips
Techniques, Tools and Tips
NaiveTestTime.MultiplyViaOperation(30000000, 0) time: 00:00:00.0001045NaiveTestTime.MultiplyViaAddition(30000000, 0) time: 00:00:00.0002622NaiveTestTime.MultiplyViaOperation(0, 30000000) time: 00:00:00.0000007NaiveTestTime.MultiplyViaAddition(0, 30000000) time: 00:00:02.9542725
Techniques, Tools and Tipsprivate static BigInteger result;
private static void Execute(BigInteger[] values, Func<BigInteger, BigInteger, BigInteger> f)
{GC.Collect();GC.WaitForPendingFinalizers();GC.Collect();
Program.result = f(new BigInteger(200), new BigInteger(300));
// ...
Techniques, Tools and Tips// ...
var operationTime = new Stopwatch();
for(var i = 0; i < values.Length / 2; i++){var a = values[i];var b = values[i + 1];operationTime.Start();Program.result = f(a, b);operationTime.Stop();
}
Console.Out.WriteLine("Execute time: {0}", operationTime.Elapsed);}
Techniques, Tools and Tips
var data = Program.Generate(50, 4);Program.Execute(data, Program.MultiplyViaOperation);Program.Execute(data, Program.MultiplyViaAddition);
»Task Manager
»Performance Monitor
»PerfView
»MiniProfiler
»CLRProfiler
»MeasureIt
»WinDbg
»Visual Studio
Techniques, Tools and Tips
http://www.machinemart.co.uk/images/library/range/large/1010.jpg
Demo: Using Tools to Analyze CodeCode Reactions – An Introduction to Reactive Extensions
»Make all compilation warning errors
»Optimize your release build
»Don’t run performance tests with the debugger attached
»Stop other applications from running during tests
»Remove exception handling (int.Parse() vs int.TryParse())
»ConfigureAwait(false) whenever possible
» If possible, consider using structs
Techniques, Tools and Tips
»Unroll LINQ statements (in certain situations)
»Limit Reflection, consider code generation scenarios
»Use WeakEventHandler
»Be aware of boxing
»Keep methods small (easier to analyze and refactor)
Techniques, Tools and Tips
Techniques, Tools and Tips
http://blogs.studentlife.utoronto.ca/gradlife/files/2011/10/library12.jpg
Improving Performance in .NET Applications
Jason BockPractice Lead
Remember… https://github.com/JasonBock/ImprovingPerformance http://www.slideshare.net/jasonbock/improving-performance-in-
net-applications-41569122 References in the notes on this slide