-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (66 loc) · 1.72 KB
/
main.go
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2023 phelmkamp. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package main
import (
"context"
"fmt"
"math/rand"
"time"
"github.com/phelmkamp/gocomps/component"
)
type props struct {
in []int
out chan int
batchSize int
}
func sum(ctx context.Context, p props) component.Component {
if len(p.in) <= p.batchSize {
return apply(ctx, p)
}
return combine(ctx, p)
}
func split(ctx context.Context, p props) component.Component {
return component.NewGroup(
component.New(sum, props{in: p.in[:len(p.in)/2], out: p.out, batchSize: p.batchSize}),
component.New(sum, props{in: p.in[len(p.in)/2:], out: p.out, batchSize: p.batchSize}),
)
}
func apply(ctx context.Context, p props) component.Component {
var n int
for _, v := range p.in {
n += v
}
p.out <- n
return component.Component{}
}
func combine(ctx context.Context, p props) component.Component {
res := make(chan int)
go func() {
var n int
for i := 0; i < 2; i++ {
n += <-res
}
p.out <- n
}()
return component.New(split, props{in: p.in, out: res, batchSize: p.batchSize})
}
func main() {
ints := make([]int, 10_000_000)
var n int
for i := 0; i < len(ints); i++ {
ints[i] = rand.Intn(5)
n += ints[i]
}
fmt.Println("want", n)
fmt.Println("\nparallel")
p := props{in: ints, out: make(chan int), batchSize: 100_000}
start := time.Now()
component.Run(context.Background(), component.New(sum, p))
fmt.Println("got", <-p.out, "in", time.Since(start))
fmt.Println("\nsequential")
p = props{in: ints, out: make(chan int)}
start = time.Now()
go component.Run(context.Background(), component.New(apply, p))
fmt.Println("got", <-p.out, "in", time.Since(start))
}