Skip to content

Instantly share code, notes, and snippets.

@tidwall
Created May 31, 2016 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tidwall/378976e24be7e2cb5a74b10eeba744eb to your computer and use it in GitHub Desktop.
Save tidwall/378976e24be7e2cb5a74b10eeba744eb to your computer and use it in GitHub Desktop.
google/btree iteration benchmark
package btree
import (
"sort"
"testing"
)
type byInts []Item
func (a byInts) Len() int {
return len(a)
}
func (a byInts) Less(i, j int) bool {
return a[i].(Int) < a[j].(Int)
}
func (a byInts) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func BenchmarkAscend(b *testing.B) {
b.StopTimer()
arr := perm(benchmarkTreeSize)
tr := New(*btreeDegree)
for _, v := range arr {
tr.ReplaceOrInsert(v)
}
sort.Sort(byInts(arr))
b.StartTimer()
for i := 0; i < b.N; i++ {
j := 0
tr.Ascend(func(item Item) bool {
if item.(Int) != arr[j].(Int) {
b.Fatalf("mismatch: expected: %v, got %v", arr[j].(Int), item.(Int))
}
j++
return true
})
}
}
func BenchmarkAscendRange(b *testing.B) {
b.StopTimer()
arr := perm(benchmarkTreeSize)
tr := New(*btreeDegree)
for _, v := range arr {
tr.ReplaceOrInsert(v)
}
sort.Sort(byInts(arr))
b.StartTimer()
for i := 0; i < b.N; i++ {
j := 100
tr.AscendRange(Int(100), arr[len(arr)-100], func(item Item) bool {
if item.(Int) != arr[j].(Int) {
b.Fatalf("mismatch: expected: %v, got %v", arr[j].(Int), item.(Int))
}
j++
return true
})
if j != len(arr)-100 {
b.Fatalf("expected: %v, got %v", len(arr)-100, j)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment