primes: add waitgroup-based parallel sieve
Dependencies
- [2]
7IEM2GFHprimes: add Erastothenes sieve solutions and tests
Change contents
- edit in snippets/primes/erastothenes_test.go at line 65
func TestWaitGroup(t *testing.T) {t.Parallel()for _, tc := range TestCases {t.Run(tc.description, func(t *testing.T) {if tc.p != erastothenes.Nth("waitgroup", tc.n) {t.FailNow()}})}} - replacement in snippets/primes/erastothenes_test.go at line 104
}[2.1767]}func BenchmarkWaitGroup(b *testing.B) {for i := 0; i < b.N; i++ {erastothenes.Nth("waitgroup", benchNum)}} - edit in snippets/primes/erastothenes/erastothenes.go at line 6
"sync" - edit in snippets/primes/erastothenes/erastothenes.go at line 65
}}return sieve}// WaitGroup based parallel sieve.func WaitGroup(limit int) []bool {sieve := make([]bool, limit+1)wg := sync.WaitGroup{}wg.Add(1)go func() {// handle even numbersfor i := 2 * 2; i <= limit; i += 2 {sieve[i] = true - edit in snippets/primes/erastothenes/erastothenes.go at line 80
wg.Done()}()for p := 3; p*p <= limit; p += 2 {wg.Add(1)go func(p int) {for i := p * p; i <= limit; i += p {sieve[i] = true}wg.Done()}(p) - edit in snippets/primes/erastothenes/erastothenes.go at line 91
wg.Wait() - edit in snippets/primes/erastothenes/erastothenes.go at line 111
case "waitgroup":s = WaitGroup(primes.MAX) - replacement in snippets/primes/erastothenes/erastothenes.go at line 122
}[2.3498]} - edit in snippets/primes/cases_test.go at line 18
3,true,},{"third prime", - edit in snippets/primes/cases_test.go at line 24
5,true,},{"forth prime",4,7,true,},{"fifth prime",5,11, - edit in snippets/primes/cases_test.go at line 43
true,},{"seventh prime",7,17,