调用的内存比预想的要多, 性能下降

This commit is contained in:
huangsimin 2019-03-14 11:39:58 +08:00
parent b435fa1221
commit 1a5b341e71
2 changed files with 36 additions and 37 deletions

View File

@ -1,8 +1,6 @@
package avl
import (
"log"
"github.com/davecgh/go-spew/spew"
"github.com/emirpasic/gods/utils"
@ -246,9 +244,6 @@ func (avl *AVL) Put(key, value interface{}) {
parent = cur
c := avl.comparator(node.key, cur.key)
if node.key.(int) == 34293 {
log.Println(c, node.key, cur.key)
}
if c > -1 { // right
child = 1
cur = cur.children[child]
@ -324,6 +319,7 @@ func (avl *AVL) lrrotate(cur *Node) {
if mov.children[l] != nil {
movparent.children[r] = mov.children[l]
movparent.children[r].parent = movparent
movparent.children[r].child = 1
} else {
movparent.children[r] = nil
@ -364,6 +360,7 @@ func (avl *AVL) rlrotate(cur *Node) {
if mov.children[l] != nil {
movparent.children[r] = mov.children[l]
movparent.children[r].parent = movparent
movparent.children[r].child = 1
} else {
movparent.children[r] = nil
@ -426,6 +423,7 @@ func (avl *AVL) rrotate(cur *Node) {
mov.height = getMaxChildrenHeight(mov) + 1
cur.height = getMaxChildrenHeight(cur) + 1
}
func (avl *AVL) lrotate(cur *Node) {

View File

@ -254,8 +254,8 @@ func TestPutComparatorRandom(t *testing.T) {
// }
// }
// const CompartorSize = 300000
// const NumberMax = 60000000
const CompartorSize = 300000
const NumberMax = 60000000
// func BenchmarkIterator(b *testing.B) {
// avl := New(utils.IntComparator)
@ -452,41 +452,25 @@ func TestPutComparatorRandom(t *testing.T) {
// // }
// }
// func BenchmarkPut(b *testing.B) {
// avl := New(utils.IntComparator)
func BenchmarkPut(b *testing.B) {
avl := New(utils.IntComparator)
// for i := 0; i < 100000; i++ {
// avl.Put(randomdata.Number(0, NumberMax))
// }
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))
// }
b.ResetTimer()
b.StartTimer()
// }
b.N = CompartorSize
for i := 0; i < b.N; i++ {
avl.Put(randomdata.Number(0, NumberMax), i)
}
}
// func BenchmarkGodsRBPut(b *testing.B) {
// rb := 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()
// avl := redblacktree.NewWithIntComparator()
// for i := 0; i < 100000; i++ {
// avl.Put(randomdata.Number(0, NumberMax), i)
@ -499,4 +483,21 @@ func TestPutComparatorRandom(t *testing.T) {
// for i := 0; i < b.N; 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)
}
}