How Many Logical Processors Used By Current Process in Golang?
Last Updated :
10 May, 2020
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
package main
import (
"fmt"
"runtime"
)
func main() {
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
package main
import (
"fmt"
"runtime"
)
func main() {
fmt.Println(runtime.GOMAXPROCS( 0 ))
}
|
Output:
8
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...