-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage_bench_test.go
102 lines (80 loc) · 2.7 KB
/
package_bench_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
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
package cantor_test
import (
"math/rand"
"testing"
"github.com/frederik-jatzkowski/cantor"
)
func BenchmarkSet_Contains(b *testing.B) {
set := buildUnionOfIntersectionsOfDifferences(2, 2, 100000)
random := rand.Int()
b.ResetTimer()
// this benchmark should not exceed on a modern CPU:
// 50 ns/op
// 0 B/op
// 0 allocs/op
for i := 0; i < b.N; i++ {
set.Contains(random)
}
}
func BenchmarkSet_Iter(b *testing.B) {
set := buildUnionOfIntersectionsOfDifferences(2, 2, 100000)
b.ResetTimer()
// this benchmark should not exceed on a modern CPU:
// 30 ms/op
// 1000 B/op
// 20 allocs/op
for i := 0; i < b.N; i++ {
set.Elements()(func(element int) (next bool) {
return true
})
}
}
func BenchmarkSet_IntoHashSet(b *testing.B) {
set := buildUnionOfIntersectionsOfDifferences(2, 2, 100000)
b.ResetTimer()
// this benchmark should not exceed on a modern CPU:
// 30 ms/op
// 800000 B/op
// 750 allocs/op
for i := 0; i < b.N; i++ {
cantor.NewHashSetFromIterator(set.Elements())
}
}
// This function builds a fairly complicated expression of set operations which is build the following way:
// The union of numberOfIntersections many intersections of numberOfIntersections many differences of two sets each,
// which were constructed using numberOfRandomSamplesPerInput integers.
// This results in an expression with numberOfIntersections*numberOfDifferences many inputs of
// size <= numberOfRandomSamplesPerInput.
//
// This expression can be used for benchmarking of different operations on a lazy evaluated set.
func buildUnionOfIntersectionsOfDifferences(
numberOfIntersections int,
numberOfDifferences int,
numberOfRandomSamplesPerInput int,
) cantor.ReadableSet[int] {
intersections := make([]cantor.ReadableSet[int], 0, numberOfIntersections)
for iIntersection := 0; iIntersection < numberOfIntersections; iIntersection++ {
differences := make([]cantor.ReadableSet[int], 0, numberOfDifferences)
for iDifference := 0; iDifference < numberOfDifferences; iDifference++ {
set1 := cantor.NewHashSet[int]()
set2 := cantor.NewHashSet[int]()
for iSample := 0; iSample < numberOfRandomSamplesPerInput; iSample++ {
set1.Add(rand.Intn(5 * numberOfRandomSamplesPerInput))
set2.Add(rand.Intn(5 * numberOfRandomSamplesPerInput))
}
differences = append(differences, set1.Intersect(set2.Complement()))
}
intersection := differences[0]
differences = differences[1:]
for _, difference := range differences {
intersection = intersection.Intersect(difference)
}
intersections = append(intersections, intersection)
}
result := intersections[0]
intersections = intersections[1:]
for _, intersection := range intersections {
result = result.Union(intersection)
}
return result
}