C# Programming Performance Tips - Part One - String Split

You can read all the C# performance tips from the following links,

C# has a total of 10 overload methods.
 
C# Programming Performance
 
Most of the developers adopt the below approach.
  1. string str = "Akshay|Patel";  
  2. Stopwatch s1 = new Stopwatch();  
  3. s1.Start();  
  4. string[] temp1 = str.Split('|');  
  5. Console.WriteLine(s1.ElapsedTicks.ToString());  

Let’s change the approach; i.e., rather than passing the character directly, let us create an array of characters and pass them as array elements.

  1. Stopwatch s2 = new Stopwatch();  
  2. s2.Start();  
  3. string[] temp = str.Split(new char[] {  
  4.     '|'  
  5. });  
  6. Console.WriteLine(s2.ElapsedTicks.ToString());  

Run the application and compare the execution time.

 C# Programming Performance

The result suggests adopting the second approach to save the execution time.