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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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())
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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))
}

chevron_right


Output:

8



My Personal Notes arrow_drop_up

Small things always make you to think big

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.