Go的时间有多精确? [英] How precise is Go's time, really?

查看:105
本文介绍了Go的时间有多精确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Go的时间包声明提供了纳秒精度。
http://golang.org/src/pkg/time/time.go



我想知道它是如何实现的,以及我是否可以信任它。我的疑惑来自Python,它清楚地记录了它随时间推移的困难和局限性:
$ b


From http://docs.python.org/2/library/time.html



各种实时函数的精度可能低于表达它们的值或参数的单位建议的

例如在大多数Unix系统中,时钟滴答只有
秒的50或100倍。另一方面,时间()和睡眠的精确度()优于
它们的Unix等价物:时间表示为浮点数,
time()返回可用的最准确时间(在可用的情况下使用Unix
gettimeofday())和sleep ()将接受一个
非零分数的时间(使用Unix select()来实现这一点,其中
可用)。

由于操作系统给python带来了如此艰难的时间,Go如何实现其纳秒级精度?

至于实现, time.Now()可以回溯到运行时实现的函数。
您可以查看 C时间实施 时间·现在在汇编中(本例中为linux amd64)。然后使用 clock_gettime ,它提供了纳秒分辨率。在Windows上,这是通过调用 GetSystemTimeAsFileTime 来实现的,也会产生纳秒(不是高分辨率,但纳秒)。

所以是的,分辨率取决于操作系统,你不能指望它在每个操作系统上都是准确的,但是开发人员正试图使其尽可能地好。例如,在go1.0.3中,FreeBSD的 time·now 使用 gettimeofday 而不是 clock_gettime ,它只提供毫秒的精度。您可以通过查看存储在 AX 中的值来查看,因为它是 syscall id 。如果你看看引用的程序集,你可以看到ms值被1000乘以得到纳秒。不过,这是固定的现在



如果您想确定,请查看运行时源代码中的相应实现,并查询操作系统的手册。 / p>

Go's time package claims giving a nanosecond precision. http://golang.org/src/pkg/time/time.go

I'm wondering how it is implemented and if I can trust it. My doubts come from Python, which clearly documents its difficulties and limitations with time:

From http://docs.python.org/2/library/time.html

The precision of the various real-time functions may be less than suggested by the units in which their value or argument is expressed. E.g. on most Unix systems, the clock "ticks" only 50 or 100 times a second.

On the other hand, the precision of time() and sleep() is better than their Unix equivalents: times are expressed as floating point numbers, time() returns the most accurate time available (using Unix gettimeofday() where available), and sleep() will accept a time with a nonzero fraction (Unix select() is used to implement this, where available).

Since the operating systems are giving such a hard time to python, how does Go achieve its nanosecond precision?

解决方案

Well as for the implementation, time.Now() falls back to a function implemented in the runtime. You can review the C time implementation and the implementation for time·now in assembly (linux amd64 in this case). This then uses clock_gettime, which provides nano seconds resolution. On windows, this is realized by calling GetSystemTimeAsFileTime, which too generates nanoseconds (not as high res but nanoseconds).

So yes, the resolution depends on the operating system and you can't expect it to be accurate on every OS but the developers are trying to make it as good as it can be. For example, in go1.0.3, time·now for FreeBSD used gettimeofday instead of clock_gettime, which only offers millisecond precision. You can see this by looking at the value stored in AX, as it is the syscall id. If you take a look at the referenced assembly, you can see that the ms value is mulitplied by 1000 to get the nanoseconds. However, this is fixed now.

If you want to be sure, check the corresponding implementations in the runtime source code and ask the manuals of your operating system.

这篇关于Go的时间有多精确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆