From 28c5f51aa9e175fb0a310a9b1dbdc774e9834745 Mon Sep 17 00:00:00 2001 From: datalore Date: Mon, 30 Sep 2024 18:28:51 +0200 Subject: [PATCH] feat(generatePrimes): number of routines now dependent on number of CPUs --- main.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 2d46a7e..73eccf9 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "fmt" "log" "os" + "runtime" "strconv" "time" ) @@ -52,12 +53,18 @@ func checkIsDivisibleByPrime(number, offset, stride int, primes *[]int, resultCh func generatePrimes(upperLimit int, loadList bool) []int { var primes []int + bootTime := time.Now() + if loadList { primes = loadPrimes() } else { primes = []int{2, 3, 5} } + numRoutines := runtime.NumCPU() + + fmt.Printf("Startup time: %v\nCalculating with %d routines\n\n", time.Now().Sub(bootTime), numRoutines) + progress := make(chan float32) go printProgress(progress, "Generating primes") @@ -73,7 +80,6 @@ func generatePrimes(upperLimit int, loadList bool) []int { checkCtx, checkCancel := context.WithCancel(context.Background()) resultChannel := make(chan bool) - numRoutines := 12 for r := 0; r < numRoutines; r++ { go checkIsDivisibleByPrime(i, r, numRoutines, &primes, resultChannel, checkCtx)