简体   繁体   中英

DateTime.Now retrieval speed

Is there any chance that this statement would return true

DateTime.Now == DateTime.Now

can a very fast machine return true for this statement, I tried on several machines and its always false ?

Yes.

The resolution of this property depends on the system timer, which is approximately 15 milliseconds on Windows systems.As a result, repeated calls to the Now property in a short time interval, such as in a loop, may return the same value.

source: https://msdn.microsoft.com/en-us/library/system.datetime.now(v=vs.110).aspx

I just ran this script in LINQPad:

Enumerable.Range(1, 1000).Select(i => DateTime.Now == DateTime.Now).ToList()

After the first element, all the others are true. I'm guessing there's some latency involved in the first couple of calls to DateTime.Now which causes the check to often return false the first time you run it in a given application.

But yes, that expression can (and often does) return true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM