Download - C# Performance Tips & Tricks .NET
![Page 1: C# Performance Tips & Tricks .NET](https://reader034.vdocuments.net/reader034/viewer/2022052505/555e283dd8b42a384f8b50a0/html5/thumbnails/1.jpg)
C# Performance
------------------------------------------------
Tips & Tricks
![Page 2: C# Performance Tips & Tricks .NET](https://reader034.vdocuments.net/reader034/viewer/2022052505/555e283dd8b42a384f8b50a0/html5/thumbnails/2.jpg)
Every developer should use a Profiler
There are some great .NET profilers out there.
Our favourites are:
- dotTrace profiler from the Jet Brains team
- Red Gate profiler
![Page 3: C# Performance Tips & Tricks .NET](https://reader034.vdocuments.net/reader034/viewer/2022052505/555e283dd8b42a384f8b50a0/html5/thumbnails/3.jpg)
The higher level abstraction you’re using, theslower it will often be.
LINQ is great for expressing something quicklythat might otherwise take a bunch of lines ofcode.
But in performance focused parts of your codebase you can be giving away too much. Especiallysince it’s so easy to chain together so manyoperations.
![Page 4: C# Performance Tips & Tricks .NET](https://reader034.vdocuments.net/reader034/viewer/2022052505/555e283dd8b42a384f8b50a0/html5/thumbnails/4.jpg)
Don’t under estimate Release builds vs. Debug builds
I’d been doing all my tests inside Visual Studio.Release builds have optimisations enabled.
I did a release build, called the methods I wastesting from a console app.
Time for some of the micro-optimisations that the.NET JIT compiler to shine!
![Page 5: C# Performance Tips & Tricks .NET](https://reader034.vdocuments.net/reader034/viewer/2022052505/555e283dd8b42a384f8b50a0/html5/thumbnails/5.jpg)
Get something running well enough in debug modeand you’re about to get some “free” performancein a release build.
- Good times!
![Page 6: C# Performance Tips & Tricks .NET](https://reader034.vdocuments.net/reader034/viewer/2022052505/555e283dd8b42a384f8b50a0/html5/thumbnails/6.jpg)
Look at the bigger picture
There are some fantastic algorithms out there.
A developer doing research before coding is aboutas likely as a developer doing proper analysisbefore writing code.
We LOVE code and always want to dive right intothe IDE.
![Page 7: C# Performance Tips & Tricks .NET](https://reader034.vdocuments.net/reader034/viewer/2022052505/555e283dd8b42a384f8b50a0/html5/thumbnails/7.jpg)
I recommend reading resources like CleverAlgorithms:
http://www.cleveralgorithms.com/
It was certainly an eye opener to me on some ofthe more advanced algorithms out there.
![Page 8: C# Performance Tips & Tricks .NET](https://reader034.vdocuments.net/reader034/viewer/2022052505/555e283dd8b42a384f8b50a0/html5/thumbnails/8.jpg)
Lets assume we have an array of arrays.
Effectively it’s a table, 3000×3000 in size. Wewant to count how slots have a value greater thanzero in them......
Memory locality matters
![Page 9: C# Performance Tips & Tricks .NET](https://reader034.vdocuments.net/reader034/viewer/2022052505/555e283dd8b42a384f8b50a0/html5/thumbnails/9.jpg)
Which of these is faster?
![Page 10: C# Performance Tips & Tricks .NET](https://reader034.vdocuments.net/reader034/viewer/2022052505/555e283dd8b42a384f8b50a0/html5/thumbnails/10.jpg)
Answer? The first one.
How much so? In my tests I got about an 8xperformance improvement on this loop!
Notice the difference? It’s the order that we’rewalking this array of arrays ([i][n] vs. [n][i]).Memory locality does indeed matter in .NET eventhough we’re well abstracted from managingmemory ourselves.
![Page 11: C# Performance Tips & Tricks .NET](https://reader034.vdocuments.net/reader034/viewer/2022052505/555e283dd8b42a384f8b50a0/html5/thumbnails/11.jpg)
CLICK HERE
enjoy this?
to read the full post