GOMAXPROCS

GOMAXPROCS is a variable that can be tuned to allow us to control the number of threads that our operating system will allocate to our goroutines within our Go binary. By default, GOMAXPROCS is equal to the number of cores available to the application. This is dynamically configurable via the runtime package. It is important to note that as of Go 1.10, GOMAXPROCS will have no upper bound limit.  

If we have a function that is CPU-intensive and parallelized (such as goroutine sorting strings), we will see some serious improvements if we adjust the number of GOMAXPROCS we have.  In the following code example, we are going to test building the standard library with a different number set for GOMAXPROCS:

#!/bin/bash
for i in 1 2 3 4
do
export GOMAXPROCS=$i
printf " Build with GOMAXPROCS=$i:"
time go build -a std
done

In our results, we can see what happens when we manipulate the total number of GOMAXPROCS:  

Realistically, we should never set GOMAXPROCS manually. There are rare occasions where you might want to limit CPU utilization for a particular binary based on the resources you have available to you on a system, or you may really need to optimize based on the resources you have on hand. For most cases, however, the default GOMAXPROCS value is sane.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.20.224.107