Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is DateTime.Now an I/O bound operation?

Tags:

c#

.net

datetime

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.

like image 676
Water Cooler v2 Avatar asked Jun 05 '16 15:06

Water Cooler v2


People also ask

Is datetime now static?

DateTime. Today is static readonly . So supposedly it should never change once (statically) instantiated.

How fast is datetime now?

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.


2 Answers

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.

like image 131
Hans Passant Avatar answered Sep 19 '22 21:09

Hans Passant


You seem to be concerned with blocking. There are two cases where you'd want to avoid that.

  1. On the UI thread it's about latency. It does not matter what you do (IO or CPU), it can't take long. Otherwise it freezes the UI thread. UtcNow is super fast so it's not a concern.
  2. Sometimes, non-blocking IO is being uses as a way to scale throughput as more load is added. Here, the only reason is to save threads because each thread consumes a lot of resources. Since there is no async way to call 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.

like image 45
usr Avatar answered Sep 19 '22 21:09

usr