-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmarks_test.go
54 lines (44 loc) · 961 Bytes
/
benchmarks_test.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
package main
import (
floodfill "leetcode/Easy/FloodFill"
"math/rand"
"testing"
"time"
)
var num = 1000
func BenchmarkLoopNormal(b *testing.B) {
for i := 0; i < b.N; i++ {
LoopNormal(num)
}
}
func BenchmarkLoopChannel(b *testing.B) {
for i := 0; i < b.N; i++ {
LoopChannel(num)
}
}
var sr, sc, color = 1, 0, 2
var image = [][]int{}
func seedImage(m, n int) {
rand.Seed(time.Now().UnixNano())
// Generate a random number between 0, 1, and 2
randomNum := rand.Intn(3)
for i := 0; i < m; i++ {
image = append(image, []int{})
for j := 0; j < n; j++ {
image[i] = append(image[i], randomNum)
}
}
}
func BenchmarkFloodFill(b *testing.B) {
seedImage(10, 10)
b.Run("FloodFillLoop", func(b *testing.B) {
for i := 0; i < b.N; i++ {
floodfill.FloodFillLoop(image, sr, sc, color)
}
})
b.Run("FloodFillRecursive", func(b *testing.B) {
for i := 0; i < b.N; i++ {
floodfill.FloodFillRecursive(image, sr, sc, color)
}
})
}