C# Performance Tricks: How To Radically Speed Up Your Code
A couple of years ago I was living and working in Belgium. I joined a small team of SharePoint specialists to build a custom application for a financial institution in Brussels. We were writing a lot of hand-coded C#.
I worked with a colleague who was fresh out of college with a computer science education under his belt. But I noticed something very interesting: even though he could write perfectly fine and functional C# code, nobody had ever taken the time to teach him how to write efficient code.
Here’s an example. Take a look at these two code snippets:
Which code snippet do you think is the fastest?
Now, to be fair, my colleague had heard of a StringBuilder. But nobody had ever explained to him what it actually does, so he didn’t have enough information to make an informed choice between the left snippet or the right.
Seasoned C# developers will know that the code on the left is extremely inefficient for large loops because in C# strings are optimized for comparing, not for modifying. The StringBuilder class is optimized for modifying, so you’re supposed to use the snippet on the right.
But let’s face it: the NET Framework is absolutely huge. For any given problem there are many competing solutions, and it is not always clear which solution is the best choice.
I created this Performance Tricks course specifically for people like my colleague: developers who already have a working knowledge of C# programming, but who want to learn how to write fast and efficient code.
“Mark Farragher’s explanations are clear, thorough, and well-thought-out.”
“Excellent course !!! Very useful and interesting content. Clear explanations of the content.”
“The teacher explains concepts very well. The lectures and the slides are well presented.”
“Excellent course. Very valuable knowledge. Clear explanations.”
“Essential material that I could not find elsewhere on the internet.”
“A very clear course with many eye openers.”
“This is an awesome course to improve performance in C# application.”
“A eye-opening look under the hood of your code.”
In the course, I cover many common performance bottlenecks. I introduce each problem with a small test program to measure the baseline performance. Then I’ll demonstrate a couple of possible solution, and benchmark how each solution measures up.
You will learn... the fundamentals of the NET Framework how to avoid Boxing a fast method to manipulate strings how to read compiled C# code which array type is the fastest how to very quickly construct objects how to use pointers in C# what the penalty of throwing exceptions is how the Garbage Collector works how to allocate arrays on the stack much more!
I added a section on Common Intermediate Code (CIL), the ‘machine’ language that the C# compiler compiles to. The CIL language is very easy to read and understand, I cover all the basics in a short 15-minute lecture.
Being able to read CIL code is an extremely useful skill that will help you analyze the output of the C# compiler, and help you avoid many performance pitfalls of the NET Framework.