调用的内存比预想的要多, 性能下降
This commit is contained in:
parent
b435fa1221
commit
1a5b341e71
|
@ -1,8 +1,6 @@
|
||||||
package avl
|
package avl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
"github.com/emirpasic/gods/utils"
|
"github.com/emirpasic/gods/utils"
|
||||||
|
@ -246,9 +244,6 @@ func (avl *AVL) Put(key, value interface{}) {
|
||||||
|
|
||||||
parent = cur
|
parent = cur
|
||||||
c := avl.comparator(node.key, cur.key)
|
c := avl.comparator(node.key, cur.key)
|
||||||
if node.key.(int) == 34293 {
|
|
||||||
log.Println(c, node.key, cur.key)
|
|
||||||
}
|
|
||||||
if c > -1 { // right
|
if c > -1 { // right
|
||||||
child = 1
|
child = 1
|
||||||
cur = cur.children[child]
|
cur = cur.children[child]
|
||||||
|
@ -324,6 +319,7 @@ func (avl *AVL) lrrotate(cur *Node) {
|
||||||
|
|
||||||
if mov.children[l] != nil {
|
if mov.children[l] != nil {
|
||||||
movparent.children[r] = mov.children[l]
|
movparent.children[r] = mov.children[l]
|
||||||
|
movparent.children[r].parent = movparent
|
||||||
movparent.children[r].child = 1
|
movparent.children[r].child = 1
|
||||||
} else {
|
} else {
|
||||||
movparent.children[r] = nil
|
movparent.children[r] = nil
|
||||||
|
@ -364,6 +360,7 @@ func (avl *AVL) rlrotate(cur *Node) {
|
||||||
|
|
||||||
if mov.children[l] != nil {
|
if mov.children[l] != nil {
|
||||||
movparent.children[r] = mov.children[l]
|
movparent.children[r] = mov.children[l]
|
||||||
|
movparent.children[r].parent = movparent
|
||||||
movparent.children[r].child = 1
|
movparent.children[r].child = 1
|
||||||
} else {
|
} else {
|
||||||
movparent.children[r] = nil
|
movparent.children[r] = nil
|
||||||
|
@ -426,6 +423,7 @@ func (avl *AVL) rrotate(cur *Node) {
|
||||||
|
|
||||||
mov.height = getMaxChildrenHeight(mov) + 1
|
mov.height = getMaxChildrenHeight(mov) + 1
|
||||||
cur.height = getMaxChildrenHeight(cur) + 1
|
cur.height = getMaxChildrenHeight(cur) + 1
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (avl *AVL) lrotate(cur *Node) {
|
func (avl *AVL) lrotate(cur *Node) {
|
||||||
|
|
|
@ -254,8 +254,8 @@ func TestPutComparatorRandom(t *testing.T) {
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// const CompartorSize = 300000
|
const CompartorSize = 300000
|
||||||
// const NumberMax = 60000000
|
const NumberMax = 60000000
|
||||||
|
|
||||||
// func BenchmarkIterator(b *testing.B) {
|
// func BenchmarkIterator(b *testing.B) {
|
||||||
// avl := New(utils.IntComparator)
|
// avl := New(utils.IntComparator)
|
||||||
|
@ -452,41 +452,25 @@ func TestPutComparatorRandom(t *testing.T) {
|
||||||
// // }
|
// // }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// func BenchmarkPut(b *testing.B) {
|
func BenchmarkPut(b *testing.B) {
|
||||||
// avl := New(utils.IntComparator)
|
avl := New(utils.IntComparator)
|
||||||
|
|
||||||
// for i := 0; i < 100000; i++ {
|
for i := 0; i < 100000; i++ {
|
||||||
// avl.Put(randomdata.Number(0, NumberMax))
|
avl.Put(randomdata.Number(0, NumberMax), i)
|
||||||
// }
|
}
|
||||||
|
|
||||||
// b.ResetTimer()
|
b.ResetTimer()
|
||||||
// b.StartTimer()
|
b.StartTimer()
|
||||||
// b.N = CompartorSize
|
|
||||||
// for i := 0; i < b.N; i++ {
|
|
||||||
// avl.Put(randomdata.Number(0, NumberMax))
|
|
||||||
// }
|
|
||||||
|
|
||||||
// }
|
b.N = CompartorSize
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
avl.Put(randomdata.Number(0, NumberMax), i)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// func BenchmarkGodsRBPut(b *testing.B) {
|
// func BenchmarkGodsRBPut(b *testing.B) {
|
||||||
// rb := redblacktree.NewWithIntComparator()
|
// avl := redblacktree.NewWithIntComparator()
|
||||||
|
|
||||||
// for i := 0; i < 100000; i++ {
|
|
||||||
// rb.Put(randomdata.Number(0, NumberMax), i)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// b.ResetTimer()
|
|
||||||
// b.StartTimer()
|
|
||||||
|
|
||||||
// b.N = CompartorSize
|
|
||||||
// for i := 0; i < b.N; i++ {
|
|
||||||
// rb.Put(randomdata.Number(0, NumberMax), i)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func BenchmarkGodsPut(b *testing.B) {
|
|
||||||
// avl := avltree.NewWithIntComparator()
|
|
||||||
|
|
||||||
// for i := 0; i < 100000; i++ {
|
// for i := 0; i < 100000; i++ {
|
||||||
// avl.Put(randomdata.Number(0, NumberMax), i)
|
// avl.Put(randomdata.Number(0, NumberMax), i)
|
||||||
|
@ -499,4 +483,21 @@ func TestPutComparatorRandom(t *testing.T) {
|
||||||
// for i := 0; i < b.N; i++ {
|
// for i := 0; i < b.N; i++ {
|
||||||
// avl.Put(randomdata.Number(0, NumberMax), i)
|
// avl.Put(randomdata.Number(0, NumberMax), i)
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
func BenchmarkGodsPut(b *testing.B) {
|
||||||
|
avl := avltree.NewWithIntComparator()
|
||||||
|
|
||||||
|
for i := 0; i < 100000; i++ {
|
||||||
|
avl.Put(randomdata.Number(0, NumberMax), i)
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
b.StartTimer()
|
||||||
|
|
||||||
|
b.N = CompartorSize
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
avl.Put(randomdata.Number(0, NumberMax), i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user