英文:
Timing an operation without being affected by clock changes
问题
我正在寻找:
1: startTime := time.Now()
2: // 在这里运行需要一段时间的代码(以毫秒为单位)
3: duration := time.Since(startTime)
然而,我需要一种不受时钟时间更改影响的方法。如果在第1行和第3行之间调整了时间,那么持续时间将不准确。
解决这个问题的一些常见方法是什么,有哪些相关的Go库可以使用?
谢谢
英文:
I'm looking for:
1: startTime := time.Now()
2: // run something here that takes a while (measured in milliseconds)
3: duration := time.Since(startTime)
However, I need something that is immune to clock time changes. If the time is adjusted between lines 1 and 3, the duration will be inaccurate.
What are some common approaches for solving this problem, and what Go libraries could be relevant?
Thanks
答案1
得分: 2
对于Linux(AMD64),Go使用clock_gettime
和CLOCK_REALTIME
。请参阅time·now
的实现。
你可能需要一个单调时钟(CLOCK_MONOTONIC
或CLOCK_MONOTONIC_RAW
),它是一个不会倒退的时钟。在Linux中,man页面明确告诉你CLOCK_MONOTONIC
不能保证不会向前跳跃:
> 这个时钟不受系统时间的不连续跳跃的影响(例如,如果系统管理员手动更改时钟),但会受到adjtime(3)和NTP执行的增量调整的影响。
因此,在Linux下,最好的选择可能是CLOCK_MONOTONIC_RAW
。你可以使用clock包来实现。示例代码如下:
import (
"fmt"
"github.com/davecheney/junk/clock"
"time"
)
func main() {
start := clock.Monotonic.Now()
// 工作
end := clock.Monotonic.Now()
duration := end.Sub(start)
fmt.Println("Elapsed:", duration)
}
进一步阅读:
英文:
For Linux (AMD64) go uses clock_gettime
with CLOCK_REALTIME
.
See the time·now
implementation.
You would want a monotonic clock (CLOCK_MONOTONIC
or CLOCK_MONOTONIC_RAW
), which
is a clock that does not go back in time. In Linux the man page explicitly tells you that CLOCK_MONOTONIC
does not guarantee to not leap forward:
> This clock is not affected by discontinuous jumps in the system time (e.g., if the system administrator
manually changes the clock), but is affected by the incremental adjustments performed by adjtime(3) and NTP.
So, under Linux, the best choice is probably CLOCK_MONOTONIC_RAW
. You may use the
clock package mentioned by @MatrixFrog for that. Example:
import (
"fmt"
"github.com/davecheney/junk/clock"
"time"
)
func main() {
start := clock.Monotonic.Now()
// work
end := clock.Monotonic.Now()
duration := end.Sub(start)
fmt.Println("Elapsed:", duration)
}
Further reading:
答案2
得分: 2
缺乏单调时钟的问题在issue 12914(2015年)中有详细说明。
自那时以来,在2017年8月和Go 1.9中,现在已经有了透明的单调时间支持:
time
包现在在每个Time
值中透明地跟踪单调时间,在存在壁钟调整的情况下,计算两个Time
值之间的持续时间是安全的操作。
有关详细信息,请参阅包文档和设计文档。
英文:
That lack of monotonic clock was detailed in issue 12914 (2015)
Since then, in August 2017 and Go 1.9, you now have a transparent Monotonic Time support:
> The time
package now transparently tracks monotonic time in each Time
value, making computing durations between two Time
values a safe operation in the presence of wall clock adjustments.
See the package docs and design document for details.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论