Go – 时间 – 毫秒

huangapple go评论102阅读模式
英文:

Go - time - milliseconds

问题

我需要以毫秒为单位的时间,可能会有大量的交易,所以我希望得到一个既正确又快速的解决方案。以下代码是否能够实现并且最好地完成任务?

<pre><code>
iMilli := int((time.Nanoseconds() % 1e6) / 1e3)
</code></pre>

TIA

英文:

I need the time in milliseconds for what could be a large volume of transactions, so I want something that is correct, and fast. Will the following work and do the job best? :
<pre><code>
iMilli := int((time.Nanoseconds() % 1e6) / 1e3)
</code></pre>

TIA

答案1

得分: 6

EDIT: 自从这个答案首次编写以来,Go编译器已经添加了逃逸分析代码。这使得编译器可以避免在某些情况下进行不必要的分配,包括(可能)下面描述的情况。因此,使用更直接的调用time.Nanoseconds()可能效果一样好。请自行进行性能分析。

大多数时间函数会导致堆分配(然后需要进行收集,导致应用程序暂停)。所以如果你经常查找时间,听起来你是这样的,你需要直接使用syscall.Gettimeofday()(其他时间函数最终也会调用它)。在这里查看更多信息的讨论:

http://groups.google.com/group/golang-nuts/browse_thread/thread/f2209022f43efcca?pli=1

我使用的解决方案是预先分配一个tv syscall.Timeval,每次通过我的内部循环时,我这样做:

syscall.Gettimeofday(&tv)

然后你可以用以下方式获取毫秒:

(int64(tv.Sec)*1e3 + int64(tv.Usec)/1e3)

我发现这比调用time.Nanoseconds()或其他更高级的时间函数要好得多。

英文:

EDIT: Since this answer was first written, escape analysis code has been added to the Go compilers. This allows the compiler to avoid unnecessary allocations in certain situations, including (probably) the one described below. With the latest weeklies, therefore, it may be just as good to use a more straightforward call to time.Nanoseconds(). Please do your own profiling.

Most of the time functions cause a heap allocation (that then subsequently needs to be collected, causing a pause in your application). So if you're looking up the time frequently, which it sounds like you are, you'll need to use syscall.Gettimeofday() directly (it's the function that the other time functions end up calling anyway). See the discussion here for more information:

http://groups.google.com/group/golang-nuts/browse_thread/thread/f2209022f43efcca?pli=1

The solution I'm using is to pre-allocate a tv syscall.Timeval, and each time through my inner loop I do this:

syscall.Gettimeofday(&amp;tv)

You can then get the milliseconds with:

(int64(tv.Sec)*1e3 + int64(tv.Usec)/1e3)

I've found this to perform a lot better than calling time.Nanoseconds() or one of the other higher-level time functions.

答案2

得分: 3

在一秒钟中有1e9纳秒,在一毫秒中有1e6纳秒,所以你可以这样做:

func getTimeString() string {
    now := time.Nanoseconds()
    localTime := time.SecondsToLocalTime(now/1e9)
    miliSeconds := (now % 1e9) / 1e6
    return fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d",localTime.Year,localTime.Month,localTime.Day,localTime.Hour,localTime.Minute,localTime.Second,miliSeconds)
}
英文:

There's 1e9 nanoseconds in a second, and 1e6 nanoseconds in a millisecond, so you'd do something like this:

func getTimeString() string {
    now := time.Nanoseconds()
    localTime := time.SecondsToLocalTime(now/1e9)
    miliSeconds := (now % 1e9) / 1e6
    return fmt.Sprintf(&quot;%04d-%02d-%02d %02d:%02d:%02d.%03d&quot;,localTime.Year,localTime.Month,localTime.Day,localTime.Hour,localTime.Minute,localTime.Second,miliSeconds)
}

答案3

得分: 2

你可以使用以下代码来获取从1970年6月1日起的当前时间的毫秒数

time.Now().UnixNano()%1e6/1e3
英文:

You can just use following code to get current time milliseconds after 1 Jun 1970

time.Now().UnixNano()%1e6/1e3

huangapple
  • 本文由 发表于 2011年5月28日 21:11:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/6161839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定