1
0
mirror of https://github.com/golang/go synced 2024-11-05 18:46:11 -07:00

container/intsets: add benchmark of AppendTo method.

Also:
- increase sparsity of sets in benchmarks.
- removed TODO in forEach.  Subword masks had no benefit.
- minor cleanup.

LGTM=crawshaw
R=crawshaw
CC=golang-codereviews
https://golang.org/cl/103470049
This commit is contained in:
Alan Donovan 2014-06-19 14:35:37 -04:00
parent e436e8e4aa
commit 5fe8afcb15
2 changed files with 20 additions and 9 deletions

View File

@ -178,8 +178,6 @@ func (b *block) min(take bool) int {
func (b *block) forEach(f func(int)) { func (b *block) forEach(f func(int)) {
for i, w := range b.bits { for i, w := range b.bits {
offset := b.offset + i*bitsPerWord offset := b.offset + i*bitsPerWord
// TODO(adonovan): opt: uses subword
// masks to avoid testing every bit.
for bi := 0; w != 0 && bi < bitsPerWord; bi++ { for bi := 0; w != 0 && bi < bitsPerWord; bi++ {
if w&1 != 0 { if w&1 != 0 {
f(offset) f(offset)
@ -210,10 +208,11 @@ func offsetAndBitIndex(x int) (int, uint) {
// initialized. // initialized.
// //
func (s *Sparse) start() *block { func (s *Sparse) start() *block {
if s.root.next == nil { root := &s.root
s.root.next = &s.root if root.next == nil {
s.root.prev = &s.root root.next = root
} else if s.root.next.prev != &s.root { root.prev = root
} else if root.next.prev != root {
// Copying a Sparse x leads to pernicious corruption: the // Copying a Sparse x leads to pernicious corruption: the
// new Sparse y shares the old linked list, but iteration // new Sparse y shares the old linked list, but iteration
// on y will never encounter &y.root so it goes into a // on y will never encounter &y.root so it goes into a
@ -221,7 +220,7 @@ func (s *Sparse) start() *block {
panic("A Sparse has been copied without (*Sparse).Copy()") panic("A Sparse has been copied without (*Sparse).Copy()")
} }
return s.root.next return root.next
} }
// IsEmpty reports whether the set s is empty. // IsEmpty reports whether the set s is empty.

View File

@ -466,7 +466,7 @@ func BenchmarkSparseBitVector(b *testing.B) {
for tries := 0; tries < b.N; tries++ { for tries := 0; tries < b.N; tries++ {
var x, y, z intsets.Sparse var x, y, z intsets.Sparse
for i := 0; i < 1000; i++ { for i := 0; i < 1000; i++ {
n := int(prng.Int()) % 10000 n := int(prng.Int()) % 100000
if i%2 == 0 { if i%2 == 0 {
x.Insert(n) x.Insert(n)
} else { } else {
@ -483,7 +483,7 @@ func BenchmarkHashTable(b *testing.B) {
for tries := 0; tries < b.N; tries++ { for tries := 0; tries < b.N; tries++ {
x, y, z := make(map[int]bool), make(map[int]bool), make(map[int]bool) x, y, z := make(map[int]bool), make(map[int]bool), make(map[int]bool)
for i := 0; i < 1000; i++ { for i := 0; i < 1000; i++ {
n := int(prng.Int()) % 10000 n := int(prng.Int()) % 100000
if i%2 == 0 { if i%2 == 0 {
x[n] = true x[n] = true
} else { } else {
@ -506,3 +506,15 @@ func BenchmarkHashTable(b *testing.B) {
} }
} }
} }
func BenchmarkAppendTo(b *testing.B) {
prng := rand.New(rand.NewSource(0))
var x intsets.Sparse
for i := 0; i < 1000; i++ {
x.Insert(int(prng.Int()) % 10000)
}
var space [1000]int
for tries := 0; tries < b.N; tries++ {
x.AppendTo(space[:0])
}
}