Skip to content

Commit

Permalink
go的benchmark测试demo增加并行测试
Browse files Browse the repository at this point in the history
  • Loading branch information
zq2599 committed Feb 12, 2023
1 parent fd81108 commit 205a5f4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions tutorials/benchmark-demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"
)

// 斐波拉契数列
func fib(n int) int {
if n == 0 || n == 1 {
return n
Expand Down
13 changes: 8 additions & 5 deletions tutorials/benchmark-demo/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ func Test_fib(t *testing.T) {
}
}

var result int

func BenchmarkFib(b *testing.B) {
var r int
for n := 0; n < b.N; n++ {
r = fib(30)
fib(30)
}
}

result = r
func BenchmarkParallelFib(b1 *testing.B) {
b1.RunParallel(func(pb *testing.PB) {
for pb.Next() {
fib(30)
}
})
}

const (
Expand Down

0 comments on commit 205a5f4

Please sign in to comment.