Skip to content

Commit

Permalink
[Test] Revise benchmarks for Fibonacci heap
Browse files Browse the repository at this point in the history
  • Loading branch information
nikox94 committed Feb 28, 2017
1 parent c0d8368 commit 283a4be
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
13 changes: 7 additions & 6 deletions fibheap/benchmarks.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
BenchmarkFibHeap_Enqueue-4 10000000 167 ns/op 72 B/op 1 allocs/op
BenchmarkFibHeap_DequeueMin-4 20000 229153 ns/op 167035 B/op 2 allocs/op
BenchmarkFibHeap_DecreaseKey-4 10000000 105 ns/op 80 B/op 1 allocs/op
BenchmarkFibHeap_Delete-4 10000 109693 ns/op 85836 B/op 2 allocs/op
BenchmarkFibHeap_Merge-4 5000000 268 ns/op 144 B/op 3 allocs/op
BenchmarkFibHeap_Enqueue-4 10000000 280 ns/op 64 B/op 1 allocs/op
BenchmarkFibHeap_DequeueMin-4 100 16990302 ns/op 16007168 B/op 2 allocs/op
BenchmarkFibHeap_DecreaseKey-4 20000000 900 ns/op 122 B/op 3 allocs/op
BenchmarkFibHeap_Delete-4 100 19087592 ns/op 16007168 B/op 2 allocs/op
BenchmarkFibHeap_Merge-4 3000000 482 ns/op 128 B/op 2 allocs/op
PASS
ok _/home/nikola/git/go-datastructures/fibheap 11.457s
coverage: 96.2% of statements
ok _/home/nikola/git/go-datastructures/fibheap 37.206s
34 changes: 19 additions & 15 deletions fibheap/fibheap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,23 +362,19 @@ func BenchmarkFibHeap_Enqueue(b *testing.B) {

heap := NewFloatFibHeap()

slice := make([]float64, 0, b.N)
for i := 0; i < b.N; i++ {
slice = append(slice, 2*1E10*(rand.Float64()-0.5))
}

for i := 0; i < b.N; i++ {
heap.Enqueue(slice[i])
heap.Enqueue(2 * 1E10 * (rand.Float64() - 0.5))
}
}

// Runs in O(log(N)) time
func BenchmarkFibHeap_DequeueMin(b *testing.B) {

heap := NewFloatFibHeap()
N := 1000000

slice := make([]float64, 0, b.N)
for i := 0; i < b.N; i++ {
slice := make([]float64, 0, N)
for i := 0; i < N; i++ {
slice = append(slice, 2*1E10*(rand.Float64()-0.5))
heap.Enqueue(slice[i])
}
Expand All @@ -392,27 +388,35 @@ func BenchmarkFibHeap_DequeueMin(b *testing.B) {
// Runs in O(1) amortized time
func BenchmarkFibHeap_DecreaseKey(b *testing.B) {
heap := NewFloatFibHeap()
N := 10000000

sliceFlt := make([]float64, 0, b.N)
sliceE := make([]*Entry, 0, b.N)
for i := 0; i < b.N; i++ {
sliceFlt := make([]float64, 0, N)
sliceE := make([]*Entry, 0, N)
for i := 0; i < N; i++ {
sliceFlt = append(sliceFlt, 2*1E10*(float64(i)-0.5))
sliceE = append(sliceE, heap.Enqueue(sliceFlt[i]))
}

b.ResetTimer()
offset := float64(2)
for i := 0; i < b.N; i++ {
// Change offset if b.N larger than N
if i%N == 0 && i > 0 {
offset *= float64(i / N)
}
// Shift-decrease keys
heap.DecreaseKey(sliceE[i], sliceFlt[i]-2E10)
heap.DecreaseKey(sliceE[i%N], sliceFlt[i%N]-offset)
}
}

// Runs in O(log(N)) time
func BenchmarkFibHeap_Delete(b *testing.B) {
heap := NewFloatFibHeap()
N := 1000000

sliceFlt := make([]float64, 0, b.N)
sliceE := make([]*Entry, 0, b.N)
for i := 0; i < b.N; i++ {
sliceFlt := make([]float64, 0, N)
sliceE := make([]*Entry, 0, N)
for i := 0; i < N; i++ {
sliceFlt = append(sliceFlt, 2*1E10*(float64(i)-0.5))
sliceE = append(sliceE, heap.Enqueue(sliceFlt[i]))
}
Expand Down

0 comments on commit 283a4be

Please sign in to comment.