Skip to content

Commit 04c7057

Browse files
soypatdeadprogram
authored andcommitted
add goroutine benchmark to examples
1 parent f76e8d2 commit 04c7057

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/examples/bench-goro/bench.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"machine"
5+
"runtime"
6+
"sync"
7+
"time"
8+
)
9+
10+
const N = 500000
11+
const Ngoro = 4
12+
13+
func main() {
14+
start := time.Now()
15+
var wg sync.WaitGroup
16+
wg.Add(Ngoro)
17+
for i := 0; i < Ngoro; i++ {
18+
go adder(&wg, N)
19+
}
20+
wg.Wait()
21+
elapsed := time.Since(start)
22+
goroutineCtxSwitchOverhead := (elapsed / (Ngoro * N)).String()
23+
24+
elapsedstr := elapsed.String()
25+
machine.LED.Configure(machine.PinConfig{Mode: machine.PinOutput})
26+
for {
27+
println("bench:", elapsedstr, "goroutine ctx switch:", goroutineCtxSwitchOverhead)
28+
machine.LED.High()
29+
time.Sleep(elapsed)
30+
machine.LED.Low()
31+
time.Sleep(elapsed)
32+
}
33+
}
34+
35+
func adder(wg *sync.WaitGroup, num int) {
36+
for i := 0; i < num; i++ {
37+
runtime.Gosched()
38+
}
39+
wg.Done()
40+
}

0 commit comments

Comments
 (0)