-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbenchs_test.go
More file actions
109 lines (101 loc) · 1.68 KB
/
Copy pathbenchs_test.go
File metadata and controls
109 lines (101 loc) · 1.68 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package stream
import (
"fmt"
"testing"
)
const iterations = 100
func BenchmarkImperative(b *testing.B) {
for n := 0; n < b.N; n++ {
count := 0
var result []int
for i := 0; i < iterations; i++ {
if count%3 == 0 {
result = append(result, count*count)
}
count++
}
_ = result
}
}
func BenchmarkFunctional(b *testing.B) {
for n := 0; n < b.N; n++ {
count := 0
_ = Generate(func() int {
c := count
count++
return c
}).Filter(func(n int) bool {
return n%3 == 0
}).Map(func(n int) int {
return n * n
}).Limit(iterations).ToSlice()
}
}
func BenchmarkForEach(b *testing.B) {
for n := 0; n < b.N; n++ {
count := 0
sum := 0
Generate(func() int {
c := count
count++
return c
}).Limit(iterations).ForEach(func(num int) {
sum += num
})
if sum != 4950 {
fmt.Println(sum)
b.FailNow()
}
}
}
func BenchmarkIter(b *testing.B) {
for n := 0; n < b.N; n++ {
count := 0
sum := 0
for _, num := range Generate(func() int {
c := count
count++
return c
}).Limit(iterations).Iter() {
sum += num
}
if sum != 4950 {
fmt.Println(sum)
b.FailNow()
}
}
}
func BenchmarkSeq(b *testing.B) {
for n := 0; n < b.N; n++ {
count := 0
sum := 0
for num := range Generate(func() int {
c := count
count++
return c
}).Limit(iterations).Seq() {
sum += num
}
if sum != 4950 {
fmt.Println(sum)
b.FailNow()
}
}
}
func BenchmarkIterSlice(b *testing.B) {
for n := 0; n < b.N; n++ {
count := 0
sum := 0
for _, num := range Generate(func() int {
c := count
count++
return c
}).Limit(iterations).ToSlice() {
sum += num
}
if sum != 4950 {
fmt.Println(sum)
b.FailNow()
}
}
}