<< Back to Blog

11 OCTOBER 2019

.NET Core: Using Timer to record execution time of code block

It can be useful to use a timer as a way of getting a rough gauge on how long a piece of code takes to execute.

C#

using System.Diagnostics
...
var timer = new Stopwatch();
timer.Start();

// Do something you want to profile

timer.Stop();
var elapsedMs = timer.ElapsedMilliseconds;