- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我用 C# 制作了一个小型测试应用程序,它设置了 DateTime.Now
并启动了一个 StopWatch
。每十秒我打印一次 _stopwatch.Elapsed.TotalMilliseconds
和 (DateTime.Now - _startTime).TotalMilliseconds
。
虽然我不认为两者完全相同,但令我惊讶的是它们每 20 秒线性偏离大约 1 毫秒。我假设 DateTime.Now
调用系统时钟,而 StopWatch
进行某种累积?
示例输出:
StopWatch : 0,2 DateTime : 1,0 Diff : 0,81
StopWatch : 10000,5 DateTime : 10002,6 Diff : 2,04
(...)
StopWatch : 2231807,5 DateTime : 2231947,7 Diff : 140,13
StopWatch : 2241809,5 DateTime : 2241950,2 Diff : 140,70
最佳答案
答案相对简单。
所有时钟都会漂移,这两种不同的计时机制将以不同的速率漂移。
在引擎盖下,秒表
uses this API
The Stopwatch class assists the manipulation of timing-related performance counters within managed code. Specifically, the Frequency field and GetTimestamp method can be used in place of the unmanaged Win32 APIs QueryPerformanceFrequency and QueryPerformanceCounter.
日期时间
uses this API
关于c# - 为什么 DateTime.Now 和 StopWatch 会漂移?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23390573/
我希望创建能够快速重复的功能 - 大约 50 毫秒。然而,我需要在调用中尽可能保持一致性,并且我发现 NSTimer 有点不稳定。看来我可能会从 mach_absolute_time 找到一些保持时间
根据 API 文档以及之前的 SO threads , SystemClock.elapsedRealtime() 应该即使在设备处于 sleep 状态时也能保持准确的时间。这不是我观察到的。 我编写
我想开发在谷歌地图上绘制当前位置的应用程序。在这里,我通过 LocationManager(GPS 提供商和网络提供商)的 onLocationChanged 获取位置详细信息。我能够获取位置详细信息
我有一个使用 MKMapView 的简单 iPhone 应用程序。它有一个分段控件,可以调整要缩放的区域(街道、社区、城市、州、世界)。它工作正常,但如果我缩小到州级别并回到街道,我注意到中心点已经移
我正在尝试在连接到本地 NTP 服务器的 Mac 上获取实际的 NTP 漂移。 读取/var/db/ntp.drift 文件时,根据PPM to milliseconds conversion 得到-
我有一个 AWS Cloud Formation 堆栈。我启动并停止了该堆栈中的 EC2 实例。现在堆栈已漂移,下面是漂移结果。我该如何解决这个问题,因为预期和实际都是相同的。 最佳答案 我不相信这与
我是一名优秀的程序员,十分优秀!