Sunday, February 7, 2016

Find Number of CPU Cores in Your Machine

How many CPU cores does your machine have? 

Here's a screenshot of my machine. It's showing 4 physical cores & 8 Logical Processors.




Why Cores are Important?


More cores are usually associated with better performance. From Go 1.5 onward, by default, Go programs run with GOMAXPROCS set to the number of cores available; in prior releases it defaulted to 1. This will definitely improve the performance of Go programs.


Here's the code to find out number of CPU cores/Logical Processors in your machine. The function NumCPU() returns the number of logical CPUs usable by the current process.



package main

import (
 "fmt"
 "runtime"
)

func main() {

 cores := runtime.NumCPU()

 fmt.Printf("This machine has %d CPU cores. \n", cores)

}