measure time using Stopwatch
Published: 26. September 2011 | Updated: 4. November 2011License: Microsoft Public License (MS-PL)
Categories: Debug
Tags: Debug Performance
Import namespaces
using System.Diagnostics; using System.Threading;
Code
var sw = new Stopwatch(); sw.Start(); Console.WriteLine("Is running: {0}", sw.IsRunning); // true Thread.Sleep(1000); sw.Stop(); Console.WriteLine("Is running: {0}", sw.IsRunning); // false Console.WriteLine("Elapsed time: {0}", sw.Elapsed); // TimeSpan Console.WriteLine("Elapsed milliseconds: {0}", sw.ElapsedMilliseconds); // long Console.WriteLine("Elapsed ticks: {0}", sw.ElapsedTicks); // long // gets the frequency of the timer as the number of ticks per second Console.WriteLine("Stopwatch.Frequency: {0}", Stopwatch.Frequency); // gets the current number of ticks in the timer mechanism Console.WriteLine("Stopwatch.GetTimestamp(): {0}", Stopwatch.GetTimestamp()); // the timer is/isn't based on a high-resolution performance counter Console.WriteLine("Stopwatch.IsHighResolution: {0}", Stopwatch.IsHighResolution);Console Output:
Is running: True
Is running: False
Elapsed time: 00:00:01.0003260
Elapsed milliseconds: 1000
Elapsed ticks: 3313912
Stopwatch.Frequency: 3312832
Stopwatch.GetTimestamp(): 13656531829
Stopwatch.IsHighResolution: True
Is running: False
Elapsed time: 00:00:01.0003260
Elapsed milliseconds: 1000
Elapsed ticks: 3313912
Stopwatch.Frequency: 3312832
Stopwatch.GetTimestamp(): 13656531829
Stopwatch.IsHighResolution: True
| Send us feedback about this snippet » |





