Golang : Get number of CPU cores
Problem :
You want to find the maximum number of CPU cores to maximize performance.
Solution :
Use the runtime package to find out the number of CPU cores available on local machine.
package main
import (
"fmt"
"runtime"
)
func main() {
cores := runtime.NumCPU()
fmt.Printf("This machine has %d CPU cores. \n", cores)
// maximize CPU usage for maximum performance
runtime.GOMAXPROCS(cores)
}
Reference :
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+10.4k Golang : Convert file content to Hex
+18.5k Golang : How to remove certain lines from a file
+21.7k Golang : How to read float value from standard input ?
+6.4k Golang : How to get capacity of a slice or array?
+12.9k Swift : Convert (cast) Int or int32 value to CGFloat
+15.5k Golang : Get all local users and print out their home directory, description and group id
+14.5k Golang : On enumeration
+25.5k Golang : Convert long hexadecimal with strconv.ParseUint example
+32.9k Golang : Regular Expression for alphanumeric and underscore
+20.6k Swift : Convert (cast) Int to int32 or Uint32
+14.6k Golang : How to pass map to html template and access the map's elements
+15.3k JavaScript/JQuery : Detect or intercept enter key pressed example