Stopwatch stopwatch = new Stopwatch(); // initialize the Stopwatch stopwatch.Start(); // start the Stopwatch // perform the operation stopwatch.Stop(); // stop the Stopwatch Console.WriteLine("Elapsed Time: {0}", stopwatch.Elapsed); // print out the elapsed time
Stopwatch stopwatch = new Stopwatch(); for (int i = 0; i < 10; i++) { stopwatch.Restart(); // reset and start the Stopwatch // perform the operation stopwatch.Stop(); // stop the Stopwatch Console.WriteLine("Iteration {0} Elapsed Time: {1}", i + 1, stopwatch.Elapsed); }The System.Diagnostics Stopwatch class is part of the .NET Framework class library.