Thursday, June 2, 2011

c# Tips & Trics (Code performance & optimization)

//C sharp Tips1. String string
msg += thisUser.Name;
msg +=
msg += System.DateTime.Now.ToString();
msg = "Hello, ";". Today is ";//we should use the string.Format() methodstring
thisUser.Name, DateTime.Now.ToString( ));
msg = string.Format ( "Hello, {0}. Today is {1}", //For more complicated string operations you can use the StringBuilder classStringBuilder msg =
msg.Append( thisUser.Name );
msg.Append(
msg.Append( DateTime.Now.ToString());new StringBuilder( "Hello, " );". Today is " );string
String
2. For Versus Foreach Performance
--- For loop version [Method1] ---
finalMsg = msg.ToString();static
{


{
a += array[i];
}

}
--- Foreach loop version [Method2] ---
int Method1(int[] array)int a = 0;for (int i = 0; i < array.Length; i++)return a;static
{


{
a += value;
}

}
int Method2(int[] array)int a = 0;foreach (int value in array)return a;using System;using System.Diagnostics;class
{

{

Method1(array);
Method2(array);
var s1 = Stopwatch.StartNew();


{
Method1(array);
}
s1.Stop();
var s2 = Stopwatch.StartNew();

{
Method2(array);
}
s2.Stop();
Console.WriteLine(((
1000000) / m).ToString(
Console.WriteLine(((
1000000) / m).ToString(
Console.Read();
}
}
--- Output ---
7.37 ns
8.04 ns
Results. You can see that the
This
3.--- Program that uses
Programstatic void Main()int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };const int m = 100000000;for (int i = 0; i < m; i++)for (int i = 0; i < m; i++)double)(s1.Elapsed.TotalMilliseconds *"0.00 ns"));double)(s2.Elapsed.TotalMilliseconds *"0.00 ns"));foreach version required slightly more time to execute than the for version. is because the foreach-version uses more local variable space.for-loops on input string [C#] --- using System;class
{

{
Programstatic void Main()string input = "Hi Welcome to India";//// Check each character in the string using for-loop.//

{

{
spaces1++;
}
}
int spaces1 = 0;for (int i = 0; i < input.Length; i++)if (input[i] == ' ')//// BAD: Check each character in the string with ToString calls.//

{
int spaces2 = 0;for (int i = 0; i < input.Length; i++)if (input[i].ToString() == " ") // NO{
spaces2++;
}
}
//// Write results.//Console.WriteLine(spaces1);
Console.WriteLine(spaces2);
}
}
--- Output ---
3
3
--- Benchmark description ---
Iterations: 10000000
Input string: "Hi Welcome to India"Loop bodies: Same
Character testing loop: 46.50 ns
ToString loop: 445.11 ns
Summary: Using ToString was nearly ten times slower.
4.Flattened array
--- Flattened array index computation ---
array[(Y coordinate * width) + X coordinate]
--- 2D array index computation ---
array[Y coordinate, X coordinate]
Example as in top example.while
{
Console.WriteLine(

Console.WriteLine(
(true)"Enter height: [4+]");int height = int.Parse(Console.ReadLine());"Enter width: [10+]");int width = int.Parse(Console.ReadLine());//// A. TWO DIMENSIONAL ARRAY//
int[,] twoDimensional = new int[height, width];// Assign cell 1, 6twoDimensional[1, 6] = 5;
// Assign cell 3, 9twoDimensional[3, 9] = 9;
// Assign cell at 2, 3twoDimensional[2, 3] = 1;
// Display
{

{
Console.Write(twoDimensional[i, a]);
}
Console.WriteLine();
}
Console.WriteLine();
for (int i = 0; i < height; i++)for (int a = 0; a < width; a++)//// B. FLATTENED ARRAY//
int[] oneDimensional = new int[width * height];// Assign cell 1, 6oneDimensional[1 * width + 6] = 5;
// Assign cell 3, 9oneDimensional[3 * width + 9] = 9;
// Assign cell at 2, 3oneDimensional[2 * width + 3] = 1;
// Display
{

{
Console.Write(oneDimensional[i * width + a]);
}
Console.WriteLine();
}
}
for (int i = 0; i < height; i++)for (int a = 0; a < width; a++)

No comments:

Post a Comment