Download - Improving Performance in .NET Applications
![Page 1: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/1.jpg)
Improving Performance in .NET Applications
Jason BockPractice Lead
![Page 2: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/2.jpg)
»http://www.magenic.com
»http://www.jasonbock.net
»https://www.twitter.com/jasonbock
»https://www.github.com/jasonbock
Personal Info
![Page 3: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/3.jpg)
https://github.com/JasonBock/
ImprovingPerformance
http://www.slideshare.net/jasonbock/improving-performance-in-net-applications-41569122
Downloads
![Page 4: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/4.jpg)
»Performance
»Techniques, Tools and Tips
Overview
Remember…
https://github.com/JasonBock/
ImprovingPerformance
http://www.slideshare.net/jasonbock/improving-performance-in-net-applications-41569122
![Page 5: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/5.jpg)
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
![Page 6: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/6.jpg)
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
![Page 7: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/7.jpg)
Performance
http://1.bp.blogspot.com/_EUzvVB2uRZI/TM5LHdryAoI/AAAAAAAAAJM/Dyajh-
EwctY/s1600/computer+on+fire.png
![Page 8: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/8.jpg)
Performance
http://stackexchange.com/performance
…
![Page 9: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/9.jpg)
Performance
http://www.troyhunt.com/2014/12/applied-azure-infographic-of-how-have-i.html
![Page 10: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/10.jpg)
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
![Page 11: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/11.jpg)
Performance
UI
Service
Business Logic
Data Store
Core
Message Bus
CLR
![Page 12: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/12.jpg)
»Memory management
»Garbage collection
»Code loading and execution
»Structured exception handling
»Common type system
»Security
Performance
![Page 13: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/13.jpg)
Performance
http://www.clker.com/cliparts/I/b/r/1/6/n/simple-green-check-button-hi.png
![Page 14: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/14.jpg)
Techniques, Tools and Tips
![Page 15: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/15.jpg)
Techniques, Tools and Tips
http://www.stellman-greene.com/blog/wp-content/uploads/2008/09/sally-code-review.png
![Page 16: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/16.jpg)
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)
![Page 17: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/17.jpg)
Techniques, Tools and Tips
![Page 18: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/18.jpg)
Techniques, Tools and Tips
http://groomsadvice.com/wp-content/uploads/2011/05/bigstock_Tape_Measure_14084828.jpg
![Page 19: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/19.jpg)
Techniques, Tools and Tips
http://cdn2.whatchristianswanttoknow.com/wp-content/uploads/2014/01/10-Convicting-Questions-To-Ask-Yourself-
Today.jpg
![Page 20: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/20.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;}
![Page 21: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/21.jpg)
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);
}
![Page 22: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/22.jpg)
Techniques, Tools and Tips
![Page 23: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/23.jpg)
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
![Page 24: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/24.jpg)
Techniques, Tools and Tips
![Page 25: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/25.jpg)
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
![Page 26: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/26.jpg)
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));
// ...
![Page 27: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/27.jpg)
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);}
![Page 28: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/28.jpg)
Techniques, Tools and Tips
var data = Program.Generate(50, 4);Program.Execute(data, Program.MultiplyViaOperation);Program.Execute(data, Program.MultiplyViaAddition);
![Page 29: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/29.jpg)
»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
![Page 30: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/30.jpg)
Demo: Using Tools to Analyze CodeCode Reactions – An Introduction to Reactive Extensions
![Page 31: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/31.jpg)
»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
![Page 32: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/32.jpg)
»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
![Page 33: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/33.jpg)
Techniques, Tools and Tips
http://blogs.studentlife.utoronto.ca/gradlife/files/2011/10/library12.jpg
![Page 34: Improving Performance in .NET Applications](https://reader034.vdocuments.us/reader034/viewer/2022042701/55a884b21a28abfd518b4775/html5/thumbnails/34.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