What happens when you call DateTime.Now
?
I followed the property code in Reflector and it appears to add the time zone offset of the current locale to UtcNow
. Following UTCNow
led me, turn by turn, finally to a Win32 API call.
I reflected on it and asked a related question but haven't received a satisfactory response yet. From the links in the present comment on that question, I infer that there is a hardware unit that keeps time. But I also want to know what unit it keeps time in and whether or not it uses the CPU to convert time into a human readable unit. This will shed some light on whether the retrieval of date and time information is I/O bound or compute-bound.
DateTime. Today is static readonly . So supposedly it should never change once (statically) instantiated.
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.
You are deeply in undocumented territory with this question. Time is provided by the kernel: the underlying native API call is NtQuerySystemTime()
. This does get tinkered with across Windows versions - Windows 8 especially heavily altered the underlying implementation, with visible side-effects.
It is I/O bound in nature: time is maintained by the RTC (Real Time Clock) which used to be a dedicated chip but nowadays is integrated in the chipset. But there is very strong evidence that it isn't I/O bound in practice. Time updates in sync with the clock interrupt so very likely the interrupt handler reads the RTC and you get a copy of the value. Something you can see when you tinker with timeBeginPeriod()
.
And you can see when you profile it that it only takes ~7 nanoseconds on Windows 10 - entirely too fast to be I/O bound.
You seem to be concerned with blocking. There are two cases where you'd want to avoid that.
UtcNow
is super fast so it's not a concern.UtcNow
the question is moot. You just have to call it as is.Since time on Windows usually advances at 60 Hz I'd assume that a call to UtcNow
reads from an in-memory variable that is written to at 60 Hz. That makes is CPU bound. But it does not matter either way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With