Efficient Stopwatch usage

New .NET 7 feature to make StopWatch usage more efficient

Home DailyDrop

Daily Knowledge Drop

.NET 7 introduced new StopWatch functionality, which not only makes using the class easier.


Typical usage

The StopWatch class is often used to perform simple benchmarks to determine how long a piece of code executes for. The StopWatch is started, the code in question is executed, and then the StopWatch is stopped and the ElapsedTime is captured.

Typically the StopWatch class is used as follows:

// new instance of Stopwatch
Stopwatch sw = new Stopwatch();
sw.Start();

await Task.Delay(500);

sw.Stop();
Console.WriteLine($"Time Elapsed: {sw.Elapsed}");

Improved usage

The issue with the above is that memory is allocated for the StopWatch instance declared. Instead, a more memory efficient approach is to do the following:

// get the start time (in ticks)
long startTime = Stopwatch.GetTimestamp();

await Task.Delay(500);

// get the end time (in ticks)
long endTime = Stopwatch.GetTimestamp();
// calculate the difference
TimeSpan elapsedTime = new TimeSpan(endTime - startTime);
Console.WriteLine($"Time Elapsed: {elapsedTime}");

No memory is allocated in this example, but it is slightly more complex to code than the previous example.


.NET 7 improvements

A new method, Stopwatch.GetElapsedTime, was introduced with .NET 7 which makes the above approach simpler:

// get the start time (in ticks)
long startTime = Stopwatch.GetTimestamp();

await Task.Delay(500);

// get the elapsed time between the startTime and the currentTime
TimeSpace elapsedTime = Stopwatch.GetElapsedTime(startTime);
Console.WriteLine($"Time Elapsed: {elapsedTime}");

The GetElapsedTime method will calculate the elapsed time between the startTime and the current time (it is also possible to explicitly supply an endTime to the method as well).

Here we get the benefit of less memory allocations, and also not having to do additional calculations.


Notes

A small, useful new method which makes using StopWatch cleaner and more efficient overall - one doesn't want diagnostic tools to be detrimental to the performance and readability of the application code. Having said that, having a single StopWatch instance should really not impact application performance at all - however its always a good practice to be as efficient as possible, traded off with effort, and in this case, there is no additional effort required to be slightly more efficient.


References

Are you using the Stopwatch efficiently in .NET?

Daily Drop 228: 05-01-2023

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net stopwatch benchmark