2010 18 Mar
Software Developers phobia is code optimization, how to write faster codes? How to make programs runs faster at the same hardware? How to make your client happy?
Heaving read a few million lines of C# code, I came up with these optimization benchmarks that help to significantly improve code performance through simple changes.
1. Boxing and unboxing
static private void TestBoxingAndUnboxing() {
int i = 123;
object o = i; // Implicit boxing
i = 456; // Change the contents of i
int j = (int)o; // Unboxing (may throw an exception if the types are incompatible)
}
//this function is about 2*Log(N) faster
static private void TestNoBoxingAndUnboxing()
{
int i = 123;
i = 456; // Change the contents of i
int j = i; // Compatible types
}
2. Collections
//Using actual size for data rather than some default size increases performance up to 50%
ht = new Hashtable(count); // Creates an Hashtable using size = Count LoadData(ht, count);
// Display results
Display.Show(1, "Elapsed time: " + t.ElapsedTime.ToString() + " ms \n", 0);
// End of the demo
Display.Show(-1, "Collections demo end.\n", 1);
}
static private void LoadData(Hashtable ht, int Count)
{
// Fill the employee collection with data
for (int i = 0; i < Count; i++)
{
Employee employee = new Employee(i, "Employee" + i.ToString());
ht.Add(i, employee);
}
3. Exceptions
//throwing and catching an exception is 1000 slower than checking for an error code
static void FunctionThatThrows()
{
throw new Exception();
}
static int FunctionThatReturnsErrorCode()
{
return -1;
}
4. Loops
//for loop is twice faster than foreach loop
static private void TestForeachLoop(ArrayList List)
{
int id = 0;
foreach (int i in List)
{
// Do something with the object ...
id = i;
}
}
static private void TestForLoop(ArrayList List)
{
int id = 0;
int count = List.Count;
for (int i = 0; i < List.Count; i++)
{
// Do something with the object ...
id = (int)List[i];
}
}
5. Garbage collection
//80% slower than doing nothing
static private void Test(int count)
{
DisposableObject []obj = new DisposableObject[count];
for(int i=0;i<count;i++)
obj[i]=new DisposableObject();
// Do some work with the object here ...
// Release the object.
obj = null;
// Forcing garbage collection to finalize the object.
Collect();
// Wait for the GC's Finalize thread to finish.
WaitForPendingFinalizers();
}
//150% slower than doing nothing
static private void TestDispose(int count)
{
DisposableObject []obj = new DisposableObject[count];
for(int i=0;i<count;i++)
obj[i]=new DisposableObject();
// Do some work with the object here ...
// Release the object.
for(int i=0;i<count;i++)
obj[i].Dispose();
obj = null;
// Forcing garbage collection to finalize the object.
Collect();
// Wait for the GC's Finalize thread to finish.
WaitForPendingFinalizers();
}
//best solution
static private voide LetDotnetCleanup(in
t count)
{
// Start performance timing
t.Start();
int count=int.Parse(args[0]);
DisposableObject []obj = new DisposableObject[count];
for(int i=0;i<count;i++)
obj[i]=new DisposableObject();
// Do some work with the object here ...
// Release the object.
obj = null;
t.Stop();
// Stop performance timing
}
Reference http://msdn.microsoft.com/en-us/library/ms973858.aspx
Thanks for the tips. Simple, easy and important. Sometimes small issues make a wide difference in performance.



