Welcome to the "C# Performance Improvement Article Series". This is my third presentation. So if you are new to this series of articles then I will recommend you try to go through the following code.
if(!study (First && Second && Third) ){
}else{
Welcome to "5 tips to improve performance of C# code -part 3";
}
Ha.. Ha.., No, this is not my first point or I am not going to measure performance of this funny pseudo code. And like every article I repeat a few lines. I have tested the following code in my Core i3 CPU, 4GB primary memory and Windows 7 platform. If you are in a different hardware configuration or using a different platform then your output may vary with my output screen and the output may vary depending on your current running process. As all points are performance testing I have tested them in release mode and taken a screen shot in a stable situation for all the code. Let's start with tip number 1.
How you check empty string in your code?
In this point I am going to show you three empty or null string checking styles. I hope you are familiar with all styles but may not familiar with their performance. Let's start with a small example. In the following there are three functions (Yes all static. I am lazy, and don't want to create the object again). In the first style I am using the Length property. In the second I am using a space or empty string (" "). And in the third case I am using an Empty property of the string class. The following is my test code.
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- namespace Test1
- {
- public class Compare
- {
- public static void First(string Value)
- {
- if (Value.Length != 0)
- {
- }
- }
- public static void Second(string Value)
- {
- if (Value != "")
- {
- }
- }
- public static void Third(string Value)
- {
- if (Value != string.Empty )
- {
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Stopwatch sw = new Stopwatch();
- sw.Start();
- Compare.First("Sourav");
- sw.Stop();
- Console.WriteLine("Using Length: "+sw.ElapsedTicks);
- sw.Restart();
- Compare.Second("Sourav");
- sw.Stop();
- Console.WriteLine("Using != " +sw.ElapsedTicks);
- sw.Restart();
- Compare.Third("Sourav");
- sw.Stop();
- Console.WriteLine("Using Empty: " + sw.ElapsedTicks);
- Console.ReadLine();
- }
- }
- }
And the output screen is:

We can see the Length measuring style (the first one) is taking the most time. And the String.Empty style is the least time consuming process.
So, the single line conclusion "Use of String.Empty to check whether the string is null or not".
Change your style of type casting
Yes, change it if you are not implementing the proper type casting technique. In the following I show two traditional styles of type casting and their performance impact on code. The first style (and the worst) is very simple, by using parenthesees () used by most of developers. And the second style is by the as keyword. In the following code I have implemented both of them. Let's go through the following code.
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- namespace Test1
- {
- public class Base
- {
- }
- public class Derive : Base
- {
- }
- class Program
- {
- static void Main(string[] args)
- {
- Derive d = new Derive();
- Stopwatch sw = new Stopwatch();
- sw.Start();
- Base b =(Base) d;
- sw.Stop();
- Console.WriteLine("Using type cust : "+ sw.ElapsedTicks);
- sw.Restart();
- Base c = d as Base;
- sw.Stop();
- Console.WriteLine("Using as keyword : "+ sw.ElapsedTicks);
- Console.ReadLine();
- }
- }
- }
And here is the output screen:

Hmm.. Our popular worst technique is 6 times slower than the as keyword. Now, in this point I will not write single line conclusion, it's your choice of how to cast objects.
Efficient string comparison method
Yes, one more tip regarding strings. We know there are two popular string comparison methods we frequently use. One is the "operator ==" and another one is using the Equals() method of the String class.
One day I asked one of my colleagues "Which one do you like the most?" He replied the first one (in other words operator ==). And I asked him why? He replied "It's very simple, easy and from my first of C# learning I have been using it and I am happy with it, I will not change my style". Hmm, if you are also in that situation then I suggest you have a look at the output screen first then in the code.




Andrzej CzerwoniecPosted Sep 3, 2013, 7:16 AM
Conclusion in the first example isn't too precise, you are not checking for nulls anywhere, you are jsut checking for empty strings, and btw. I'd always use an IsNullOrEmpty method for that kind of checks ;]
Sam HobbsPosted Jul 13, 2013, 2:18 PM
Note that the String.Equality Operator ("operator ==") is implemented using the Equals method, so they are the same; there should be no difference.
Sam HobbsPosted Jul 13, 2013, 12:25 PM
I am still waiting or the answer to the question in part 2 of what you mean by "memory allocation graph"? What are you using for determining performance?