primes: add waitgroup-based parallel sieve

voroskoi
Oct 20, 2021, 4:41 PM
UCLDPER5MMVEP7V4UU7ZIXH7MYUZDW5MUDAJXX772Y3JDUAPOH3AC

Dependencies

  • [2] 7IEM2GFH primes: add Erastothenes sieve solutions and tests

Change contents

  • edit in snippets/primes/erastothenes_test.go at line 65
    [2.1200]
    [2.1200]
    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][2.1767:1768]()
    }
    [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
    [2.1963]
    [2.1963]
    "sync"
  • edit in snippets/primes/erastothenes/erastothenes.go at line 65
    [2.3054]
    [2.3054]
    }
    }
    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 numbers
    for i := 2 * 2; i <= limit; i += 2 {
    sieve[i] = true
  • edit in snippets/primes/erastothenes/erastothenes.go at line 80
    [2.3058]
    [2.3058]
    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
    [2.3061]
    [2.3061]
    wg.Wait()
  • edit in snippets/primes/erastothenes/erastothenes.go at line 111
    [2.3403]
    [2.3403]
    case "waitgroup":
    s = WaitGroup(primes.MAX)
  • replacement in snippets/primes/erastothenes/erastothenes.go at line 122
    [2.3498][2.3498:3500]()
    }
    [2.3498]
    }
  • edit in snippets/primes/cases_test.go at line 18
    [2.3731]
    [2.3731]
    3,
    true,
    },
    {
    "third prime",
  • edit in snippets/primes/cases_test.go at line 24
    [2.3736]
    [2.3736]
    5,
    true,
    },
    {
    "forth prime",
    4,
    7,
    true,
    },
    {
    "fifth prime",
    5,
    11,
  • edit in snippets/primes/cases_test.go at line 43
    [2.3779]
    [2.3779]
    true,
    },
    {
    "seventh prime",
    7,
    17,