C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Start: The Start method tells the Stopwatch to store the current time. It queries Windows to find the current tick count of the system.
Stop: We call Stop on the instance Stopwatch. This tells the Stopwatch to capture the current tick count of the system.
Elapsed: The Elapsed property on Stopwatch is a TimeSpan struct, which overrides the ToString method.
TimeSpanToStringC# program that uses Stopwatch
using System;
using System.Diagnostics;
using System.Threading;
class Program
{
static void Main()
{
// Create new stopwatch.
Stopwatch stopwatch = new Stopwatch();
// Begin timing.
stopwatch.Start();
// Do something.
for (int i = 0; i < 1000; i++)
{
Thread.Sleep(1);
}
// Stop timing.
stopwatch.Stop();
// Write result.
Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);
}
}
Output
Time elapsed: 00:00:01.0001477
Format: We use Elapsed with a format string. We must escape the ":" characters in the format string. We use hh, mm and ss.
Note: Thanks to Mike Keller for the suggestion. Nanoseconds are not helpful for timing long-running tasks.
C# program that uses hours, minutes, seconds output
using System;
using System.Diagnostics;
class Program
{
static void Main()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
// ... This takes 10 seconds to finish.
for (int i = 0; i < 1000; i++)
{
System.Threading.Thread.Sleep(10);
}
// Stop.
stopwatch.Stop();
// Write hours, minutes and seconds.
Console.WriteLine("Time elapsed: {0:hh\\:mm\\:ss}", stopwatch.Elapsed);
}
}
Output
Time elapsed: 00:00:10
Info: This method is called to create a new instance of Stopwatch. We show the var syntax, which is a shorthand for the type declaration.
VarC# program that uses StartNew
class Program
{
static void Main()
{
// Create new stopwatch.
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
// Do something (omitted).
// Stop timing.
stopwatch.Stop();
// Write the results (omitted).
}
}
Here: The example code first creates a new Stopwatch with StartNew, and then captures the ElapsedTicks long property twice.
LongWriteLine: The values are printed to the screen. The Console.WriteLine here was the most expensive operation. It took 7081 ticks.
Note: The ElapsedTicks value from Stopwatch is not normally the same as the DateTime.Ticks value.
Note 2: Stopwatch ticks are far more accurate, when the IsHighResolution property is true. Otherwise, they are equivalent.
C# program that uses ElapsedTicks
using System;
using System.Diagnostics;
class Program
{
static void Main()
{
// Create a new Stopwatch.
var stopwatch = Stopwatch.StartNew();
// Capture the elapsed ticks and write them to the console.
long ticks1 = stopwatch.ElapsedTicks;
Console.WriteLine(ticks1);
// Capture the ticks again.
// ... This will be a larger value.
long ticks2 = stopwatch.ElapsedTicks;
Console.WriteLine(ticks2);
}
}
Output
11
7092
Reset: Reset, meanwhile, only sets the timer information to zero. It does not call Start on the Stopwatch.
So: After Reset, we need to explicitly call Start. In this program, try replacing Restart with Reset.
C# program that uses Restart method
using System;
using System.Diagnostics;
using System.Threading;
class Program
{
static void Main()
{
Stopwatch stop = Stopwatch.StartNew();
Thread.Sleep(1000);
stop.Restart();
Thread.Sleep(1000);
Console.WriteLine(stop.ElapsedMilliseconds);
}
}
Output
1000
Result: Using a Stopwatch is more expensive than simple operations in the .NET Framework.
And: For this reason, Stopwatch itself can become a performance problem. We should not use it in production code in tight loops.
Context: For context, simple additions and multiplications require only a couple nanoseconds on most systems.
C# program that tests Stopwatch execution time
using System;
using System.Diagnostics;
class Program
{
const int _max = 1000000;
static void Main()
{
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
var sw = Stopwatch.StartNew();
sw = null;
}
s1.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
Console.Read();
}
}
Output
600.64 ns
Frequency: This returns the number of ticks the Stopwatch uses per second. It is used to convert ElapsedTicks to a figure in seconds.
IsHighResolution: Tells whether the Stopwatch is using high resolution timing. Stopwatch isn't as useful when this is false.
Stopwatch properties:
Stopwatch.Frequency: 14318180
Stopwatch.IsHighResolution: True
Stopwatch.GetTimestamp: 174412139895