.NET
How to Measure Time Correctly in .NET (via Nick Chapsas)
- Capturing two
DateTime.UtcNow
values and subtracting isn’t the most accurate approach because there are many other considerations going on (e.g., leap seconds). - When using the
Stopwatch
class, there’s some overhead for having to allocate it on the heap. - In .NET 7 we got
Stopwatch.GetTimestamp()
which returns a 64-bit value. This gets a value from the operation system. - Final Answer: Use
StopWatch.GetTimestamp()
withStopwatch.GetElapsedTime(myStartTime)
.
The Best Way to Work with Units in .NET (via Nick Chapas)
- When dealing with units of measurement, check out the NuGet library UnitsNet.