benchmark Insert and Remove

This commit is contained in:
Andrew Gillis 2018-04-29 18:21:27 -04:00
parent 91a80481f6
commit bc0d5bf324
2 changed files with 54 additions and 36 deletions

View file

@ -152,25 +152,26 @@ func (q *Deque) Insert(i int, elem interface{}) {
return return
} }
if i <= q.count/2 { if i <= q.count/2 {
// If inserting closer to front, rotate front to back i places, put // If inserting closer to front, put element at front, then swap
// element at front, then rotate back to front i places. // elements until new element at specified position.
for j := 0; j < i; j++ {
q.PushBack(q.PopFront())
}
q.PushFront(elem) q.PushFront(elem)
mod := (len(q.buf) - 1)
var x, y int
for j := 0; j < i; j++ { for j := 0; j < i; j++ {
q.PushFront(q.PopBack()) x = (q.head + j) & mod
y = (q.head + j + 1) & mod
q.buf[x], q.buf[y] = q.buf[y], q.buf[x]
} }
} else { } else {
// If inserting closer to back, rotate back to front Len() - i places, // If inserting closer to back, put element at back, then swap
// put element at back, then rotate front to back same amount. // elements until new element at specified position.
rots := q.count - i
for j := 0; j < rots; j++ {
q.PushFront(q.PopBack())
}
q.PushBack(elem) q.PushBack(elem)
for j := 0; j < rots; j++ { mod := (len(q.buf) - 1)
q.PushBack(q.PopFront()) var x, y int
for j := q.count - 1; j > i; j-- {
x = (q.head + j) & mod
y = (q.head + j - 1) & mod
q.buf[x], q.buf[y] = q.buf[y], q.buf[x]
} }
} }
} }
@ -189,31 +190,26 @@ func (q *Deque) Remove(i int) interface{} {
if i == q.count-1 { if i == q.count-1 {
return q.PopBack() return q.PopBack()
} }
var elem interface{} mod := len(q.buf) - 1
var x, y int
if i <= q.count/2 { if i <= q.count/2 {
// If removing closer to front, rotate front to back i places, remove // If removing closer to front, swap until element is at front and then
// element at front, then rotate back to front i places. // remove it.
for j := 0; j < i; j++ { for ; i > 0; i-- {
q.PushBack(q.PopFront()) x = (q.head + i - 1) & mod
} y = (q.head + i) & mod
elem = q.PopFront() q.buf[x], q.buf[y] = q.buf[y], q.buf[x]
for j := 0; j < i; j++ {
q.PushFront(q.PopBack())
}
} else {
// If removing closer to back, rotate back to front Len() - 1 - i
// places, remove element at back, then rotate front to back same
// amount.
rots := (q.count - 1) - i
for j := 0; j < rots; j++ {
q.PushFront(q.PopBack())
}
elem = q.PopBack()
for j := 0; j < rots; j++ {
q.PushBack(q.PopFront())
} }
return q.PopFront()
} }
return elem // If removing closer to back, swap until element is at back and then
// remove it.
for ; i < q.count-1; i++ {
x = (q.head + i) & mod
y = (q.head + i + 1) & mod
q.buf[x], q.buf[y] = q.buf[y], q.buf[x]
}
return q.PopBack()
} }
// Replace replaces the element at position i with the given element. Accepts // Replace replaces the element at position i with the given element. Accepts

View file

@ -595,3 +595,25 @@ func BenchmarkRotate(b *testing.B) {
} }
} }
} }
func BenchmarkInsert(b *testing.B) {
var q Deque
for i := 0; i < b.N; i++ {
q.PushBack(i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
q.Insert(q.Len()/2, -i)
}
}
func BenchmarkRemove(b *testing.B) {
var q Deque
for i := 0; i < b.N; i++ {
q.PushBack(i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
q.Remove(q.Len() / 2)
}
}