This repository was archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparallel.go
More file actions
61 lines (54 loc) · 1.41 KB
/
parallel.go
File metadata and controls
61 lines (54 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package loop
import (
"context"
"sync"
)
// Parallel provides the ability to range over a slice concurrently.
// Each element of the slice will be called within it's own goroutine.
//
// This function should not be used in hopes to speed up any pure compute
// operation as there is an associated cost with spawning a new goroutine.
// Instead, it makes sense if there are any long running tasks inside of
// your loop.
//
// The benchmarks in parallel_test.go show a good example of when this
// method will speed up performance. (using time.Sleep)
func Parallel[E any](xs []E) func(func(int, E) bool) {
return func(yield func(int, E) bool) {
for i := range ParallelTimes(len(xs)) {
x := xs[i]
if !yield(i, x) {
break
}
}
}
}
// ParallelTimes allows you to peform a parallel iteration for a given
// integer type.
//
// This is very similar to the loop.Parallel method, except that instead
// of looping over a slice of elements, it instead will range for
// the number of given
func ParallelTimes[Int intType](num Int) func(func(Int) bool) {
return func(yield func(Int) bool) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var wg sync.WaitGroup
wg.Add(int(num))
for i := range uint64(num) {
go func() {
defer wg.Done()
select {
case <-ctx.Done():
return
default:
if !yield(Int(i)) {
cancel()
return
}
}
}()
}
wg.Wait()
}
}