golang 在 1 秒内每 1 毫秒执行一次函数(每秒调用 1000 次)

标签 go milliseconds

我是 golang 的新手。

我想在特定持续时间(现在为 1 秒)内精确地以 1 毫秒调用一个函数,该函数应该每秒调用 1000 次。

我确实使用 timer 和 tick 遵循并实现了多种方法,但是当测试一个每次调用时只增加一个值的简单函数时,我发现它在 1 秒内只被调用了大约 630 次。

当我在循环中调用该函数并在 1 秒后停止它时,该函数在 1 秒内被调用约 ~ 226370427 次,这意味着我的计时器和滴答有问题。

这是我使用 goroutine 并发实现的代码,它使用 ticker 调用每 1 毫秒递增一次的函数,并在 1 秒后由 main 停止,然后使用 int channel 读取总和值。

import (
    "fmt"
    "time"
)

func main() {
    tickChan := time.NewTicker(time.Millisecond * 1).C

    doneChan := make(chan bool)
    resultChan := make(chan int)
    go start(tickChan, doneChan, resultChan)
    time.Sleep(time.Second * 1)
    doneChan <- true // Set it to true to stop the infinte loop in the routine
    fmt.Printf("total is : %v\n", <-resultChan)

}

func start(tickChan <-chan time.Time, doneChan <-chan bool, resultChan chan int) {
    sum := 0 // <-- The the value to increment
    for {
        select {
        case <-tickChan:
            sum++
        case <-doneChan:
            resultChan <- sum
            return
        }
    }
}

我能够在 node.js 中成功地做到这一点(使用 nanotimer lib)并且能够在每 1 毫秒内在 1 秒内准确地运行 1000 次,我什至也在微秒内完成了它(大约是 ~ 988676在 1 秒而不是 100 万 )

编辑:

这里是我的go版本 go version go1.10.2 windows/amd64

然后去环境 `

set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\name\AppData\Local\go-build
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\name\Documents\Go Projects
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\name\AppData\Local\Temp\go-build403228958=/tmp/go-build -gno-record-gcc-switches

`

编辑 2:我在 Playground 上对其进行了测试,并设法获得了 1000 个总数,我认为问题出在 Windows 上。有什么办法可以解决它吗?

最佳答案

Package time

import "time"

type Ticker

A Ticker holds a channel that delivers `ticks' of a clock at intervals.

type Ticker struct {
        C <-chan Time // The channel on which the ticks are delivered.
        // contains filtered or unexported fields
}

func NewTicker

func NewTicker(d Duration) *Ticker

NewTicker returns a new Ticker containing a channel that will send the time with a period specified by the duration argument. It adjusts the intervals or drops ticks to make up for slow receivers. The duration d must be greater than zero; if not, NewTicker will panic. Stop the ticker to release associated resources.


这是我在同一台双引导机器上针对 Windows 10.0.17134 和 Linux 4.15 的结果。

package main

import (
    "fmt"
    "runtime"
    "time"
)

func ticker(tick time.Duration) {
    ticker := time.NewTicker(tick)
    defer ticker.Stop()
    done := make(chan bool)
    sleep := 1 * time.Second
    go func() {
        time.Sleep(sleep)
        done <- true
    }()
    ticks := 0
    for {
        select {
        case <-done:
            fmt.Printf("%v × %v ticks in %v\n", ticks, tick, sleep)
            return
        case <-ticker.C:
            ticks++
        }
    }
}

func main() {
    fmt.Println("GOMAXPROCS:", runtime.GOMAXPROCS(0))
    start := time.Duration(1 * time.Millisecond)
    end := start / 1000
    for tick := start; tick >= end; tick /= 10 {
        ticker(tick)
    }
}

输出:

Microsoft Windows [Version 10.0.17134.112]:

GOMAXPROCS: 4
554 × 1ms ticks in 1s
552 × 100µs ticks in 1s
542 × 10µs ticks in 1s
552 × 1µs ticks in 1s

Linux 4.15.0-23-generic:

GOMAXPROCS: 4
1000 × 1ms ticks in 1s
9990 × 100µs ticks in 1s
13169 × 10µs ticks in 1s
14014 × 1µs ticks in 1s

Sleep function | Microsoft Docs

Suspends the execution of the current thread until the time-out interval elapses.

Parameters

dwMilliseconds

The time interval for which execution is to be suspended, in milliseconds.


在 Windows 上,Go Ticker 分辨率可能最多限制为一毫秒。

关于golang 在 1 秒内每 1 毫秒执行一次函数(每秒调用 1000 次),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50994524/

相关文章:

c# - 数据表日期时间列没有毫秒

function - 在单独的 golang 包中声明一个结构不能返回值,但在具体声明时可以

go - 序列化结构字段以预先存在 byte slice

php - PHP 中以毫秒为单位的日期时间到时间戳

sql - 将毫秒转换为时间戳

MongoDB:将日期字符串 (mm/dd/yyyy) 转换为 Unix 时间戳

c++11 - std::chrono::milliseconds .count() 以微秒为单位返回?

go - 如何在 http.Request 中获取 URL

go - 检查持续集成的格式

Go环境变量设置后保持不变