Using Stopwatch class in C#

gpeipman
1,145 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Stopwatch class

Stopwatch class may seem like primitive class that makes some date math behind the scenes but it actually doesn't hold true. With Stopwatch class it is possible to make very accurate measurements if operating system and computer hardware support high-resolution performance counter. If counter is not supported then fallback to simple date math is made.

Let's see simple program that uses Stopwatch class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Diagnostics;
using System.Threading.Tasks;
class Hello
{
static void Main()
{
var watch = new Stopwatch();
watch.Start();
using (var task = Task.Delay(2000))
{
task.Wait();
}
watch.Stop();
Console.WriteLine("Time elapsed: " + watch.Elapsed);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

This code can be written a little shorter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Diagnostics;
using System.Threading.Tasks;
class Hello
{
static void Main()
{
var watch = Stopwatch.StartNew();
using (var task = Task.Delay(2000))
{
task.Wait();
}
watch.Stop();
Console.WriteLine("Time elapsed: " + watch.Elapsed);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Notice one important thing: timer is stopped before time elapsed is written out. If we leave timer running the result is probably not the same.

Checking for high resolution mode

If something depends on if Stopwatch is using high resolution mode then IsHighResolution() of Stopwatch class can be called like shown in the following example.

1
2
3
4
5
6
7
8
9
10
using System;
using System.Diagnostics;
class Hello
{
static void Main()
{
Console.WriteLine("IsHighResolution: " + Stopwatch.IsHighResolution);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Starting and stopping stopwatch

Stopwatch is better than primitive date math also because it's possible to stop and reset it. Stopping means that internal clock doesn't tick. Reset means that ticks clock is set to zero and counting of ticks starts over.

Next example uses short for-loop to wait one second while stopwatch is running and one second when stopwatch is stopped. In total the example runs around four seconds. As time is counted only for two waits then stopwatch reports elapsed time as something around two seconds.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;
using System.Diagnostics;
using System.Threading.Tasks;
class Hello
{
static void Main()
{
var watch = Stopwatch.StartNew();
for(var i = 0; i < 2; i++)
{
using (var task = Task.Delay(1000))
{
task.Wait();
}
watch.Stop();
using (var task = Task.Delay(1000))
{
task.Wait();
}
watch.Start();
}
watch.Stop();
Console.WriteLine("Time elapsed: " + watch.Elapsed);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Helper method for measuring code running time

Now let's write simple helper method that measures for how long given delegate expression runs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.Diagnostics;
using System.Threading.Tasks;
class Hello
{
static void Main()
{
var time = StopwatchHelper.MeasureRunTime(() =>
{
for(var i = 0; i < 2; i++)
{
using (var task = Task.Delay(1000))
{
task.Wait();
}
}
});
Console.WriteLine("Time elapsed: " + time);
}
static class StopwatchHelper
{
public static TimeSpan MeasureRunTime(Action codeToRun)
{
var watch = Stopwatch.StartNew();
codeToRun();
watch.Stop();
return watch.Elapsed;
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

References

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content