I realize this is way too far into the micro-optimization area, but I am curious to understand why Calls to DateTime.Now and DateTime.UtcNow are so \"expensive\". I have a s
For an up-to-date look on the profiled speed of DateTime.UtcNow / DateTimeOffset.UtcNow, see this dotnet thread, where BenchmarkDotNet was used to profile.
There was unfortunately a perf regression with the jump to .NET (Core) 3 compared to 2.2, but even reporting with the regressed value, DateTime.UtcNow is coming in at a pretty rocking time of 71 ns (it had been 25 ns), i.e. 71 billionths of a second.
To put that in perspective, even at the slower speed of 71ns, it means:
You can call DateTime.UtcNow ~ 14,000 times for a cost of only 1 millisecond!
At the previously faster time of 25 ns (hopefully they will get this performance back), you can call DateTime.UtcNow ~ 40,000 times for a cost of 1 millisecond.
I'm not looking at old .NET Framework times here, but at least with the newer bits, I think it can safely be stated that it is at least no longer accurate to say that DateTime.UtcNow is "slow/expensive" (I appreciate that the question was asked however!).