How Many Logical Processors Used By Current Process in Golang?
The task is to find the number of logical CPUs the process can be running on at a given time. Here are the few examples.
Approach 1: Using the NumCPU function
NumCPU returns the number of logical CPUs usable by the current process.
Syntax:
func NumCPU() int
Example :
Go
// Golang program to find the // number of logical processors // used by current process package main import ( "fmt" "runtime" ) // Main function func main() { // Using the NumCPU function fmt.Println(runtime.NumCPU()) } |
Output:
8
Approach 2: Using the GOMAXPROCS function
GOMAXPROCS sets the maximum number of CPUs that can be executing simultaneously and returns the previous setting.
Syntax:
func GOMAXPROCS(n int) int
Example :
Go
// Golang program to find the // number of logical processors // used by current process package main import ( "fmt" "runtime" ) // Main function func main() { // Using the GOMAXPROCS function fmt.Println(runtime.GOMAXPROCS( 0 )) } |
Output:
8