- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在研究具有8核的机器(带有“2,8 GHz Intel Core i7”处理器的Mac),可以看到正在运行fmt.Println(runtime.NumCPU())
。
我实现了一个非常简单的工作程序池模型,以同时处理一些进入该池的请求。进程类型为“CPU密集型”,我想对使用更多GO内核的性能有多大的了解。
所以代码如下
func Run(poolSize int, workSize int, loopSize int, maxCores int) {
runtime.GOMAXPROCS(maxCores)
var wg sync.WaitGroup
wg.Add(poolSize)
defer wg.Wait()
// this is the channel where we write the requests for work to be performed by the pool
workStream := make(chan int)
// cpuIntensiveWork simulates an CPU intensive process
var cpuIntensiveWork = func(input int) {
res := input
for i := 0; i < loopSize; i++ {
res = res + i
}
}
// worker is the function that gets fired by the pool
worker := func(wg *sync.WaitGroup, workStream chan int, id int) {
defer wg.Done()
for req := range workStream {
cpuIntensiveWork(req)
}
}
// launch the goroutines of the pool
for i := 0; i < poolSize; i++ {
go worker(&wg, workStream, i)
}
// feed the workStream until the end and then close the channel
for workItemNo := 0; workItemNo < workSize; workItemNo++ {
workStream <- workItemNo
}
close(workStream)
}
基准是这些
var numberOfWorkers = 100
var numberOfRequests = 1000
var loopSize = 100000
func Benchmark_1Core(b *testing.B) {
for i := 0; i < b.N; i++ {
Run(numberOfWorkers, numberOfRequests, loopSize, 1)
}
}
func Benchmark_2Cores(b *testing.B) {
for i := 0; i < b.N; i++ {
Run(numberOfWorkers, numberOfRequests, loopSize, 2)
}
}
func Benchmark_4Cores(b *testing.B) {
for i := 0; i < b.N; i++ {
Run(numberOfWorkers, numberOfRequests, loopSize, 4)
}
}
func Benchmark_8Cores(b *testing.B) {
for i := 0; i < b.N; i++ {
Run(numberOfWorkers, numberOfRequests, loopSize, 8)
}
}
运行基准测试时,我注意到性能几乎从1核增加到2核再到4核呈线性增长。但是我从4核升级到8核的性能差异非常有限。
最佳答案
有了多个核心,事情可能会变得有趣。最可能的解释是您没有八个核心,但是四个具有超线程的核心将为您带来更少的加速-有时根本没有。
另一个可能需要检查的解释是,每个线程都使用大量内存,并且您用完了缓存内存。否则您将达到内存带宽饱和的状态,此时没有任何处理器可以为您提供帮助。
关于go - 性能几乎以线性方式提高,将GOMAXPROCS从1增加到4,但此后保持平稳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64121524/
我很好奇 Go 运行时如何确定 runtime.NumCPU()(CPU 数)在具有截然不同特性的各种平台(如 Linux、macOS 和 Windows)上? 例如在 Linux 上:我很好奇它是否
不设置同名环境变量时,是否保证GOMAXPROCS设置为1? 此代码显示值: package main import ( "runtime" "fmt" ) func getGOMAX
我有一个简单的 go 程序 - main.go - package main import ( "log" "runtime" "time" ) func main() {
我试图在主线程中同时运行一个 goroutine(go 版本 go1.4.1 darwin/amd64)和无限循环,但我无法让它工作。如果我理解正确的话,如果我指定了 GOMAXPROCS,go 应该
我们都知道runtime.GOMAXPROCS默认设置为CPU核心数,如果这个属性设置的太大了怎么办? 程序会有更多上下文切换吗? 是否会更频繁地触发垃圾收集器? 最佳答案 GOMAXPROCS 默认
我已经设置了 runtime.GOMAXPROCS(2),但是这个程序在输出一些数字时仍然挂起。我可以看到这个程序使用了高 cpu(超过 100%),但我不明白为什么 for 循环 goroutine
我在 MacBook 和 Ubuntu 上玩 golang 1.7.3,发现 runtime.GOMAXPROCS 被限制为 256。有人知道这个限制是从哪里来的吗?这是否记录在任何地方,为什么会有限
我见过有人将 runtime.GOMAXPROCS 设置为 runtime.NumCPU() 以启用并行 处理去。 Official documentation没有说明 GOMAXPROCS 的上限;
我正在尝试理解 Golang 中的一个 typical data races从多个 goroutines 访问未 protected 全局变量可能会导致竞争条件: var service map[st
在下面的 GoLang 程序中,我试图实现 stable marriage problem对于 N 个男人和 N 个女人,使用 2*N 个协程(每个男人和女人 1 个)。 程序严格遵循程序定义,因为每
The GOMAXPROCS variable limits the number of operating system threads that can execute user-level Go
对不起,如果这听起来很愚蠢。 如果在 runtime.NumCpu() == 2 时运行 runtime.GOMAXPROCS(4) 会发生什么? 最佳答案 runtime.GOMAXPROCS 控制
我正在玩 Go,在我的 2015 8 核 MacBookPro 上运行 go 1.7.3。 尝试了解 runtime.GOMAXPROCS 设置为其最大值 (256) 并启动相同数量的 gorouti
我只是想试试goroutine切换上下文的速度有多快,所以写了下面的代码。令我惊讶的是,多个 gorountines 比不需要切换上下文的版本运行得更快(我将程序设置为仅在一个 CPU 内核中运行)。
我是一名优秀的程序员,十分优秀!