Click here to Skip to main content
15,895,084 members
Articles / Programming Languages / C#

A High-Precision Stopwatch for C#

Rate me:
Please Sign up or sign in to vote.
4.87/5 (22 votes)
17 Jan 2006CPOL1 min read 106.4K   3K   39  
This article presents a stopwatch class with microsecond-precision for C# that offers split-time and a System.TimeSpan interface.
using System;

namespace Stopwatch_Demo
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Stopwatch_Test
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			Performance.Stopwatch sw = new Performance.Stopwatch();

			sw.Start();

			for(int i=0; i<10; i++)
			{
				System.Threading.Thread.Sleep(100);
				Console.Write("Split time: ");
				Console.Write(sw.GetSplitTimeInMicroseconds().ToString());
				Console.WriteLine(" microseconds.");
			}

			sw.Stop();

			Console.Write("Total process time: ");
			Console.Write(sw.GetElapsedTimeSpan().ToString());
			Console.WriteLine(".");
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Aspen Insights
United States United States
Walter Storm is currently doing quantitative research and data science. Originally from Tunkhannock, PA., he has a B.S. in Aerospace Engineering from Embry-Riddle Aeronautical University[^], and an M.S. in Systems Engineering from SMU[^]. He has been professionally developing software in some form or another since January of 2001.

View Walter Storm's profile on LinkedIn.[^]

Comments and Discussions