简体   繁体   English

DateTime.Now检索速度

[英]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 ? 一个非常快的机器可以返回true这个声明,我试过几台机器,它总是假的?

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. 此属性的分辨率取决于系统计时器,在Windows系统上约为15毫秒。因此,在短时间间隔(例如循环)中重复调用Now属性可能会返回相同的值。

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

I just ran this script in LINQPad: 我刚刚在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. 我猜测在DateTime.Now的前几次调用中涉及到一些延迟,这导致检查在第一次在给定应用程序中运行时经常返回false。

But yes, that expression can (and often does) return true. 但是,是的,那个表达式可以(并且经常会)返回true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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