updated benchmark

This commit is contained in:
Jae Kwon 2014-05-21 19:06:43 -07:00
parent f7873d6e2b
commit 850144f3e7
3 changed files with 20 additions and 27 deletions

View File

@ -53,12 +53,12 @@ func TestImmutableAvlPutHasGetRemove(t *testing.T) {
var val Value var val Value
var updated bool var updated bool
ranrec := func() *record { randomRecord := func() *record {
return &record{ randstr(20), randstr(20) } return &record{ randstr(20), randstr(20) }
} }
for i := range records { for i := range records {
r := ranrec() r := randomRecord()
records[i] = r records[i] = r
tree, updated = tree.Put(r.key, String("")) tree, updated = tree.Put(r.key, String(""))
if updated { if updated {
@ -121,27 +121,23 @@ func BenchmarkImmutableAvlTree(b *testing.B) {
value String value String
} }
records := make([]*record, 100) randomRecord := func() *record {
return &record{ randstr(32), randstr(32) }
ranrec := func() *record {
return &record{ randstr(20), randstr(20) }
} }
for i := range records { t := NewIAVLTree()
records[i] = ranrec() for i:=0; i<1000000; i++ {
r := randomRecord()
t.Put(r.key, r.value)
} }
b.StartTimer() b.StartTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
t := NewIAVLTree() r := randomRecord()
for _, r := range records {
t.Put(r.key, r.value) t.Put(r.key, r.value)
}
for _, r := range records {
t.Remove(r.key) t.Remove(r.key)
} }
} }
}
func TestTraversals(t *testing.T) { func TestTraversals(t *testing.T) {
@ -161,11 +157,9 @@ func TestTraversals(t *testing.T) {
} }
j := 0 j := 0
for itr := Iterator(T.Root());
tn, next := Iterator(T.Root())(); for node := itr(); node != nil; node = itr() {
next != nil; if int(node.Key().(Int)) != data[j] {
tn, next = next () {
if int(tn.Key().(Int)) != data[j] {
t.Error("key in wrong spot in-order") t.Error("key in wrong spot in-order")
} }
j += 1 j += 1
@ -206,9 +200,8 @@ func TestGriffin(t *testing.T) {
t.Fatalf("Expected %v new hashes, got %v", hashCount, count) t.Fatalf("Expected %v new hashes, got %v", hashCount, count)
} }
// nuke hashes and reconstruct hash, ensure it's the same. // nuke hashes and reconstruct hash, ensure it's the same.
var node Node itr := Iterator(n2)
for itr:=Iterator(n2); itr!=nil; { for node:=itr(); node!=nil; node = itr() {
node, itr = itr()
if node != nil { if node != nil {
node.(*IAVLNode).hash = nil node.(*IAVLNode).hash = nil
} }

View File

@ -41,7 +41,7 @@ type Node interface {
Remove(key Key) (_ *IAVLNode, value Value, err error) Remove(key Key) (_ *IAVLNode, value Value, err error)
} }
type NodeIterator func() (node Node, next NodeIterator) type NodeIterator func() (node Node)
func NotFound(key Key) error { func NotFound(key Key) error {
return fmt.Errorf("Key was not found.") return fmt.Errorf("Key was not found.")

View File

@ -8,7 +8,7 @@ func Iterator(node Node) NodeIterator {
stack := make([]Node, 0, 10) stack := make([]Node, 0, 10)
var cur Node = node var cur Node = node
var tn_iterator NodeIterator var tn_iterator NodeIterator
tn_iterator = func()(tn Node, next NodeIterator) { tn_iterator = func()(tn Node) {
if len(stack) > 0 || cur != nil { if len(stack) > 0 || cur != nil {
for cur != nil { for cur != nil {
stack = append(stack, cur) stack = append(stack, cur)
@ -17,9 +17,9 @@ func Iterator(node Node) NodeIterator {
stack, cur = pop(stack) stack, cur = pop(stack)
tn = cur tn = cur
cur = cur.Right() cur = cur.Right()
return tn, tn_iterator return tn
} else { } else {
return nil, nil return nil
} }
} }
return tn_iterator return tn_iterator