Skip to content

Commit 283a4be

Browse files
committed
[Test] Revise benchmarks for Fibonacci heap
1 parent c0d8368 commit 283a4be

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

fibheap/benchmarks.txt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
BenchmarkFibHeap_Enqueue-4 10000000 167 ns/op 72 B/op 1 allocs/op
2-
BenchmarkFibHeap_DequeueMin-4 20000 229153 ns/op 167035 B/op 2 allocs/op
3-
BenchmarkFibHeap_DecreaseKey-4 10000000 105 ns/op 80 B/op 1 allocs/op
4-
BenchmarkFibHeap_Delete-4 10000 109693 ns/op 85836 B/op 2 allocs/op
5-
BenchmarkFibHeap_Merge-4 5000000 268 ns/op 144 B/op 3 allocs/op
1+
BenchmarkFibHeap_Enqueue-4 10000000 280 ns/op 64 B/op 1 allocs/op
2+
BenchmarkFibHeap_DequeueMin-4 100 16990302 ns/op 16007168 B/op 2 allocs/op
3+
BenchmarkFibHeap_DecreaseKey-4 20000000 900 ns/op 122 B/op 3 allocs/op
4+
BenchmarkFibHeap_Delete-4 100 19087592 ns/op 16007168 B/op 2 allocs/op
5+
BenchmarkFibHeap_Merge-4 3000000 482 ns/op 128 B/op 2 allocs/op
66
PASS
7-
ok _/home/nikola/git/go-datastructures/fibheap 11.457s
7+
coverage: 96.2% of statements
8+
ok _/home/nikola/git/go-datastructures/fibheap 37.206s

fibheap/fibheap_test.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -362,23 +362,19 @@ func BenchmarkFibHeap_Enqueue(b *testing.B) {
362362

363363
heap := NewFloatFibHeap()
364364

365-
slice := make([]float64, 0, b.N)
366365
for i := 0; i < b.N; i++ {
367-
slice = append(slice, 2*1E10*(rand.Float64()-0.5))
368-
}
369-
370-
for i := 0; i < b.N; i++ {
371-
heap.Enqueue(slice[i])
366+
heap.Enqueue(2 * 1E10 * (rand.Float64() - 0.5))
372367
}
373368
}
374369

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

378373
heap := NewFloatFibHeap()
374+
N := 1000000
379375

380-
slice := make([]float64, 0, b.N)
381-
for i := 0; i < b.N; i++ {
376+
slice := make([]float64, 0, N)
377+
for i := 0; i < N; i++ {
382378
slice = append(slice, 2*1E10*(rand.Float64()-0.5))
383379
heap.Enqueue(slice[i])
384380
}
@@ -392,27 +388,35 @@ func BenchmarkFibHeap_DequeueMin(b *testing.B) {
392388
// Runs in O(1) amortized time
393389
func BenchmarkFibHeap_DecreaseKey(b *testing.B) {
394390
heap := NewFloatFibHeap()
391+
N := 10000000
395392

396-
sliceFlt := make([]float64, 0, b.N)
397-
sliceE := make([]*Entry, 0, b.N)
398-
for i := 0; i < b.N; i++ {
393+
sliceFlt := make([]float64, 0, N)
394+
sliceE := make([]*Entry, 0, N)
395+
for i := 0; i < N; i++ {
399396
sliceFlt = append(sliceFlt, 2*1E10*(float64(i)-0.5))
400397
sliceE = append(sliceE, heap.Enqueue(sliceFlt[i]))
401398
}
402399

400+
b.ResetTimer()
401+
offset := float64(2)
403402
for i := 0; i < b.N; i++ {
403+
// Change offset if b.N larger than N
404+
if i%N == 0 && i > 0 {
405+
offset *= float64(i / N)
406+
}
404407
// Shift-decrease keys
405-
heap.DecreaseKey(sliceE[i], sliceFlt[i]-2E10)
408+
heap.DecreaseKey(sliceE[i%N], sliceFlt[i%N]-offset)
406409
}
407410
}
408411

409412
// Runs in O(log(N)) time
410413
func BenchmarkFibHeap_Delete(b *testing.B) {
411414
heap := NewFloatFibHeap()
415+
N := 1000000
412416

413-
sliceFlt := make([]float64, 0, b.N)
414-
sliceE := make([]*Entry, 0, b.N)
415-
for i := 0; i < b.N; i++ {
417+
sliceFlt := make([]float64, 0, N)
418+
sliceE := make([]*Entry, 0, N)
419+
for i := 0; i < N; i++ {
416420
sliceFlt = append(sliceFlt, 2*1E10*(float64(i)-0.5))
417421
sliceE = append(sliceE, heap.Enqueue(sliceFlt[i]))
418422
}

0 commit comments

Comments
 (0)