Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created June 24, 2013 07:25
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhellberg/5848304 to your computer and use it in GitHub Desktop.
Save peterhellberg/5848304 to your computer and use it in GitHub Desktop.
Find out the max value for GOMAXPROCS on your machine.
package main
import (
"fmt"
"runtime"
)
func MaxParallelism() int {
maxProcs := runtime.GOMAXPROCS(0)
numCPU := runtime.NumCPU()
if maxProcs < numCPU {
return maxProcs
}
return numCPU
}
func main() {
fmt.Println("MaxParallelism: ", MaxParallelism())
}
@peterhellberg
Copy link
Author

The result on my MacBook Air:

$ GOMAXPROCS=10 go run max_parallelism.go
MaxParallelism:  4

On the Raspberry Pi (Cross-compiled using GOARCH=arm GOARM=5 GOOS=linux go build max_parallelism.go)

$ GOMAXPROCS=10 ./max_parallelism
MaxParallelism:  1

@peruviangopher
Copy link

peruviangopher commented Oct 19, 2022

Thanks, in MacBook Air M2 this is the result:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment