我是 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 上。有什么办法可以解决它吗?
最佳答案
import "time"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(d Duration) *TickerNewTicker 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
dwMillisecondsThe 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/
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了
我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试
如何检查Ruby文件是否是通过“require”或“load”导入的,而不是简单地从命令行执行的?例如:foo.rb的内容:puts"Hello"bar.rb的内容require'foo'输出:$./foo.rbHello$./bar.rbHello基本上,我想调用bar.rb以不执行puts调用。 最佳答案 将foo.rb改为:if__FILE__==$0puts"Hello"end检查__FILE__-当前ruby文件的名称-与$0-正在运行的脚本的名称。 关于ruby-检查是否