对 [NSDate date] 的相邻调用并不总是产生不同的时间戳?
Adjacent calls to [NSDate date] do not always produce different timestamps?
我注意到我的一个 iOS 应用程序单元测试间歇性地失败,并深入研究以找出发生了什么。受影响的代码路径可以简化为:
NSDate *someDate = [NSDate date];
// Make a method call which accepts someDate as a param.
// Inside the method, check the param for null, and then:
NSDate *currentDate = [NSDate date];
NSTimeInterval interval = [currentDate timeIntervalSinceDate:someDate];
在我的机器上,interval
时不时地结束为零,即使 [NSDate date]
的两次调用之间有方法调用和空检查。我 运行 在具有 1000 次迭代的 for 循环中进行测试,只用了大约 3 次迭代就达到了 'interval' 为零的情况。我设置了一些断点以确认日期确实相同,这不仅仅是 timeIntervalSinceDate
失去精度的产物。
这似乎是之前在 this question 中观察到的,但答案听起来我应该很少看到这种情况。不知道是不是我电脑的问题,或者OS,或者both/neither.
的改动
CPU 速度很快。 NSTimeInterval
的分辨率不是无限的。 NSDate
只是与某个参考日期的 NSTimeInterval
偏移量。如果两次调用 [NSDate date]
之间的指令执行时间少于 NSTimeInterval
提供的解决方案,则它们之间的间隔将为零。
请在调试区仔细查看 it.The 时间相同(someDate 和 currentDate 值)。但是你得到的间隔是 e-5.Interval 实际值的幂是 0.000010013580322265625
NOTE
Double has a precision of at least 15 decimal digits, whereas the precision of Float can be as little as 6 decimal digits. The appropriate floating-point type to use depends on the nature and range of values you need to work with in your code. In situations where either type would be appropriate, Double is preferred.
所以我认为如果您必须更换 OS、计算机或 both/neither。
我注意到我的一个 iOS 应用程序单元测试间歇性地失败,并深入研究以找出发生了什么。受影响的代码路径可以简化为:
NSDate *someDate = [NSDate date];
// Make a method call which accepts someDate as a param.
// Inside the method, check the param for null, and then:
NSDate *currentDate = [NSDate date];
NSTimeInterval interval = [currentDate timeIntervalSinceDate:someDate];
在我的机器上,interval
时不时地结束为零,即使 [NSDate date]
的两次调用之间有方法调用和空检查。我 运行 在具有 1000 次迭代的 for 循环中进行测试,只用了大约 3 次迭代就达到了 'interval' 为零的情况。我设置了一些断点以确认日期确实相同,这不仅仅是 timeIntervalSinceDate
失去精度的产物。
这似乎是之前在 this question 中观察到的,但答案听起来我应该很少看到这种情况。不知道是不是我电脑的问题,或者OS,或者both/neither.
的改动CPU 速度很快。 NSTimeInterval
的分辨率不是无限的。 NSDate
只是与某个参考日期的 NSTimeInterval
偏移量。如果两次调用 [NSDate date]
之间的指令执行时间少于 NSTimeInterval
提供的解决方案,则它们之间的间隔将为零。
请在调试区仔细查看 it.The 时间相同(someDate 和 currentDate 值)。但是你得到的间隔是 e-5.Interval 实际值的幂是 0.000010013580322265625
NOTE
Double has a precision of at least 15 decimal digits, whereas the precision of Float can be as little as 6 decimal digits. The appropriate floating-point type to use depends on the nature and range of values you need to work with in your code. In situations where either type would be appropriate, Double is preferred.
所以我认为如果您必须更换 OS、计算机或 both/neither。