Sunday, February 3, 2008

How to test performance in C#

Keywords: Performace test, Win32, .Net Framework


In Win32,


Low resolution: GetTickCount() Api


High resolution: QueryPerformanceCounter() and QueryPerformanceFrequency()


In .Net


Low resolution: Enviroment.TickCount, the best resolution it will give you is 10 milliseconds.


High resolution: System.Diagnostics.Stopwatch class.


Stopwatch class usage:




private void btnPerformaceTest_Click(object sender, EventArgs e)
{
System.Diagnostics.Stopwatch sw = new Stopwatch();
sw.Start();
//Process1();
sw.Stop();
System.Console.WriteLine(sw.ElapsedMilliseconds);

sw.Reset();
sw.Start();
//Process2();
sw.Stop();
System.Console.WriteLine(sw.ElapsedMilliseconds);

sw.Reset();
sw.Start();
//Process3();
sw.Stop();
System.Console.WriteLine(sw.ElapsedMilliseconds);
}

No comments: