I'm trying to optimise my genetic algorithm. This uses a lot of random number selection (random mutations, etc).
I decided to use the CPU profiler:
import (
"runtime/pprof"
)
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
func main() {
if *cpuprofile != "" {
fmt.Println(*cpuprofile)
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
_ = pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
***app logic***
I was surprised to see that one of the top contributors to the CPU usage was sync.(*Mutex).Unlock, notably because I have not used threads or goroutines at any point in my application.
Some digging revealed the bottleneck is caused by the default source in math/rand being synchronized.
Is there a faster way for me to generate random numbers with functions that are not synchronized/blocking?
True randomness/accurate pseudo-randomness is not so important for this application, but it'd be preferred if I didn't get the exact same numbers multiple times in close succession.
