TODO: iter call 太多系统内存. 导致效率低下

This commit is contained in:
huangsimin
2019-04-02 18:28:41 +08:00
parent d93208f769
commit f0f16d7516
10 changed files with 169 additions and 136 deletions

View File

@@ -292,29 +292,32 @@ func (tree *Tree) Put(value interface{}) {
return
}
cur := tree.root
parent := cur.parent
child := -1
for {
if cur == nil {
parent.children[child] = node
node.parent = parent
if node.parent.height == 0 {
tree.fixPutHeight(node.parent)
}
return
}
parent = cur
c := tree.Compare(value, cur.value)
for cur, c := tree.root, 0; ; {
c = tree.Compare(value, cur.value)
if c == 0 {
cur.value = value
return
} else if c == -1 {
if cur.children[0] == nil {
cur.children[0] = node
node.parent = cur
if cur.height == 0 {
tree.fixPutHeight(cur)
}
return
}
cur = cur.children[0]
} else {
if cur.children[1] == nil {
cur.children[1] = node
node.parent = cur
if cur.height == 0 {
tree.fixPutHeight(cur)
}
return
}
cur = cur.children[1]
}
child = (c + 2) / 2
cur = cur.children[child]
}
}

View File

@@ -397,11 +397,20 @@ func BenchmarkGet(b *testing.B) {
l := loadTestData()
b.N = len(l)
for i := 0; i < b.N; i++ {
tree.Put(l[i])
}
b.ResetTimer()
b.StartTimer()
for i := 0; i < b.N; i++ {
tree.Get(l[i])
execCount := 500
b.N = len(l) * execCount
b.Log(tree.size)
for i := 0; i < execCount; i++ {
for _, v := range l {
tree.Get(v)
}
}
}
@@ -438,7 +447,7 @@ func BenchmarkPut(b *testing.B) {
b.ResetTimer()
b.StartTimer()
execCount := 50
execCount := 1000
b.N = len(l) * execCount
for i := 0; i < execCount; i++ {
tree := New(compare.Int)