Skip to content

Commit 7bafd59

Browse files
authored
Merge pull request #917 from devlights/add-ch-buffered-speed-compare
2 parents e2c87cc + af43176 commit 7bafd59

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
app
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Result
2+
3+
## Gitpod
4+
5+
```sh
6+
$ lscpu | grep 'CPU(s):'
7+
CPU(s): 16
8+
NUMA node0 CPU(s): 0-15
9+
10+
$ task
11+
task: [default] rm -f ./app
12+
task: [default] goimports -w main.go
13+
task: [default] go build -o app main.go
14+
task: [default] time ./app -loop 500000 -inch 0 -outch 0
15+
numWorkers=16
16+
done
17+
18+
real 0m0.537s
19+
user 0m0.000s
20+
sys 0m0.000s
21+
task: [default] time ./app -loop 500000 -inch 500000 -outch 0
22+
numWorkers=16
23+
done
24+
25+
real 0m0.419s
26+
user 0m0.000s
27+
sys 0m0.000s
28+
task: [default] time ./app -loop 500000 -inch 500000 -outch 500000
29+
numWorkers=16
30+
done
31+
32+
real 0m0.284s
33+
user 0m0.000s
34+
sys 0m0.000s
35+
```
36+
37+
38+
## Chromebook
39+
40+
```sh
41+
$ lscpu | grep 'CPU(s):'
42+
CPU(s): 8
43+
44+
$ task
45+
task: [default] rm -f ./app
46+
task: [default] goimports -w main.go
47+
task: [default] go build -o app main.go
48+
task: [default] time ./app -loop 500000 -inch 0 -outch 0
49+
numWorkers=8
50+
done
51+
52+
real 0m1.264s
53+
user 0m0.000s
54+
sys 0m0.000s
55+
task: [default] time ./app -loop 500000 -inch 500000 -outch 0
56+
numWorkers=8
57+
done
58+
59+
real 0m1.127s
60+
user 0m0.000s
61+
sys 0m0.000s
62+
task: [default] time ./app -loop 500000 -inch 500000 -outch 500000
63+
numWorkers=8
64+
done
65+
66+
real 0m0.403s
67+
user 0m0.000s
68+
sys 0m0.000s
69+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# https://taskfile.dev
2+
3+
version: '3'
4+
5+
vars:
6+
LOOP_CNT: 500000
7+
ZERO: 0
8+
9+
tasks:
10+
default:
11+
cmds:
12+
- rm -f ./app
13+
- goimports -w main.go
14+
- go build -o app main.go
15+
- time ./app -loop {{.LOOP_CNT}} -inch {{.ZERO}} -outch {{.ZERO}}
16+
- time ./app -loop {{.LOOP_CNT}} -inch {{.LOOP_CNT}} -outch {{.ZERO}}
17+
- time ./app -loop {{.LOOP_CNT}} -inch {{.LOOP_CNT}} -outch {{.LOOP_CNT}}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"io"
7+
"runtime"
8+
"sync"
9+
)
10+
11+
func main() {
12+
var (
13+
loopcnt = flag.Int("loop", 10000, "loop count")
14+
incnt = flag.Int("inch", 0, "input ch buffer count, 0 is unbuffered")
15+
outcnt = flag.Int("outch", 0, "output ch buffer count, 0 is unbuffered")
16+
)
17+
flag.Parse()
18+
19+
var (
20+
wg sync.WaitGroup
21+
numWorkers = runtime.GOMAXPROCS(0)
22+
in = make(chan int, *incnt)
23+
out = make(chan string, *outcnt)
24+
)
25+
26+
go func() {
27+
defer close(in)
28+
for i := range *loopcnt {
29+
in <- i
30+
}
31+
}()
32+
33+
fmt.Printf("numWorkers=%d\n", numWorkers)
34+
wg.Add(numWorkers)
35+
36+
for i := range numWorkers {
37+
go func(id int) {
38+
defer wg.Done()
39+
40+
for j := range in {
41+
out <- fmt.Sprintf("[%d] hello world [%d]", id, j)
42+
}
43+
}(i + 1)
44+
}
45+
46+
go func() {
47+
wg.Wait()
48+
close(out)
49+
}()
50+
51+
for v := range out {
52+
fmt.Fprintln(io.Discard, v)
53+
}
54+
55+
fmt.Println("done")
56+
}

0 commit comments

Comments
 (0)