mirror of
https://github.com/golang/go
synced 2024-11-26 09:48:14 -07:00
container/heap: optimization in case heap has many duplicates
benchmark old ns/op new ns/op delta BenchmarkDup 3075682 609448 -80.18% R=gri CC=golang-dev https://golang.org/cl/6613064
This commit is contained in:
parent
4c9c36e655
commit
c12dab2aa6
@ -79,7 +79,7 @@ func Remove(h Interface, i int) interface{} {
|
||||
func up(h Interface, j int) {
|
||||
for {
|
||||
i := (j - 1) / 2 // parent
|
||||
if i == j || h.Less(i, j) {
|
||||
if i == j || !h.Less(j, i) {
|
||||
break
|
||||
}
|
||||
h.Swap(i, j)
|
||||
@ -97,7 +97,7 @@ func down(h Interface, i, n int) {
|
||||
if j2 := j1 + 1; j2 < n && !h.Less(j1, j2) {
|
||||
j = j2 // = 2*i + 2 // right child
|
||||
}
|
||||
if h.Less(i, j) {
|
||||
if !h.Less(j, i) {
|
||||
break
|
||||
}
|
||||
h.Swap(i, j)
|
||||
|
@ -170,3 +170,16 @@ func TestRemove2(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkDup(b *testing.B) {
|
||||
const n = 10000
|
||||
h := make(myHeap, n)
|
||||
for i := 0; i < b.N; i++ {
|
||||
for j := 0; j < n; j++ {
|
||||
Push(&h, 0) // all elements are the same
|
||||
}
|
||||
for h.Len() > 0 {
|
||||
Pop(&h)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user