C# - StopWatch, The high resolution timer

29. August 2011 14:06

 

Did you know that C# has a high resolution timer in the System.Diagnostics namespace which is very useful for benchmarking / fine tunning small parts of code that run regularly. Here is a short example on how to use it.

 

 

class Program
{
	static void Main(string[] args)
	{
		System.Diagnostics.Stopwatch SW = new Stopwatch();

		SW.Start();
		Console.WriteLine("Starting");
		for (int i = 0; i < 1000; i++)
			Console.WriteLine("Test");

		SW.Stop();

		Console.WriteLine("Time Taken: {0}", SW.ElapsedMilliseconds);
		Console.ReadLine();

	}
}

 

E-mail Kick it! DZone it! del.icio.us Permalink