Skip to content

Commit 1013626

Browse files
author
Stefan Nilsson
committed
Add benchmarks
1 parent ff5df92 commit 1013626

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

weak.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ func components(g Iterator) (sets disjointSets, count int) {
4545
return
4646
}
4747

48-
// Union-find with path compression performs any sequence of
49-
// m ≥ n find and n – 1 union operations in O(m log n) time.
48+
// Union-find with path compression performs any sequence of m ≥ n find
49+
// and n – 1 union operations in O(m log n) time. Union by rank doesn't
50+
// seem to improve performance here.
5051
type disjointSets []int
5152

5253
func makeSingletons(n int) disjointSets {

weak_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package graph
22

33
import (
4+
"math/rand"
45
"testing"
56
)
67

@@ -50,3 +51,29 @@ func TestComponents(t *testing.T) {
5051
t.Errorf("Connected %s", mess)
5152
}
5253
}
54+
55+
func BenchmarkConnected(b *testing.B) {
56+
n := 1000
57+
b.StopTimer()
58+
g := New(n)
59+
for i := 0; i < n; i++ {
60+
g.AddBoth(rand.Intn(n), rand.Intn(n))
61+
}
62+
b.StartTimer()
63+
for i := 0; i < b.N; i++ {
64+
_ = Connected(g)
65+
}
66+
}
67+
68+
func BenchmarkComponents(b *testing.B) {
69+
n := 1000
70+
b.StopTimer()
71+
g := New(n)
72+
for i := 0; i < n; i++ {
73+
g.AddBoth(rand.Intn(n), rand.Intn(n))
74+
}
75+
b.StartTimer()
76+
for i := 0; i < b.N; i++ {
77+
_ = Components(g)
78+
}
79+
}

0 commit comments

Comments
 (0)