C# Stopwatch
last modified July 5, 2023
C# Stopwatch tutorial shows how to measure execution time in C# using Stopwatch class.
Stopwatch
class provides a set of methods and properties that can
be used to accurately measure elapsed time. Stopwatch is part of the
System.Diagnostics
namespace.
The usage of Stopwatch
is straightforward. We start the stopwatch
with Start
and end it with Stop
. The code to be
measured is placed between these two methods. The elapsed time can be
retrieved with Elapsed
, ElapsedMilliseconds
or
ElapsedTicks
properties.
C# Stopwatch - string concatenation
We measure string concatenation methods.
using System.Diagnostics; var text = string.Empty; var sw = new Stopwatch(); sw.Start(); for (int i=0; i < 100_000; i++) { text += "abc"; } var n = text.Length; Console.WriteLine($"# of chars: {n}"); sw.Stop(); var elapsed = sw.ElapsedMilliseconds; Console.WriteLine($"Concat elapsed: {elapsed} ms");
We concatenate a string 100,000 times. We measure efficiency of the
+
operator; the elapsed time is in milliseconds.
$ dotnet run of chars: 300000 Concat elapsed: 6611 ms
In the second example, we use string interpolation.
using System.Diagnostics; using System.Text; var text = string.Empty; var sw = new Stopwatch(); sw.Start(); for (int i=0; i < 100_000; i++) { text = $"{text}abc"; } var n = text.Length; Console.WriteLine($"# of chars: {n}"); sw.Stop(); var elapsed = sw.ElapsedMilliseconds; Console.WriteLine($"Interpolate elapsed: {elapsed} ms");
The $
special character identifies a string literal as an
interpolated string.
$ dotnet run of chars: 300000 Interpolate elapsed: 6576 ms
C# Stopwatch - sorting algorithm
There are several algorithms for sorting items. We are going to compare selection sort with bubble sort.
using System.Diagnostics; var sw = new Stopwatch(); sw.Start(); DoSelectionSort(GetArray()); sw.Stop(); var elapsed = sw.ElapsedMilliseconds; Console.WriteLine($"Selection sort: {elapsed} ms"); int[] GetArray() { var rnd = new Random(); var vals = new int[30_000]; for (int i = 0; i < 30_000; i++) { vals[i] = rnd.Next(1, 100); } return vals; } void DoSelectionSort(int[] a) { int len = a.Length; for (int i = 0; i < len - 1; i++) { int min_idx = i; for (int j = i + 1; j < len; j++) { if (a[j] < a[min_idx]) { min_idx = j; } } int temp = a[min_idx]; a[min_idx] = a[i]; a[i] = temp; } }
We create a array having 30,000 random values. The elements are sorted with the selection sort.
$ dotnet run Selection sort: 1871 ms
In the second example, we measure the bubble sort.
using System.Diagnostics; var sw = new Stopwatch(); sw.Start(); DoBubbleSort(GetArray()); sw.Stop(); var elapsed = sw.ElapsedMilliseconds; Console.WriteLine($"Bubble sort: {elapsed} ms"); int[] GetArray() { var rnd = new Random(); var vals = new int[30_000]; for (int i = 0; i < 30_000; i++) { vals[i] = rnd.Next(1, 100); } return vals; } void DoBubbleSort(int[] a) { int len = a.Length; for (int i = 0; i < len - 1; i++) { for (int j = 0; j < len - i - 1; j++) { if (a[j] > a[j + 1]) { int temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } }
The bubble sort is a less efficient algorithm, so the it should take longer to sort the array with bubble sort.
$ dotnet run Bubble sort: 4368 ms
Source
Stopwatch class - language reference
In this article we have measured execution time of C# programs with
Stopwatch
.
Author
List all C# tutorials.