summaryrefslogtreecommitdiff
path: root/sort/bench.go
blob: 094debdc083306b21210f4412cfed07099eb5321 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package sort

import (
	"algorithms/ds"
	"fmt"
	"testing"
)

// Avoid compiler optimizations
var benchResult []ds.Comparer

func benchmark(name string, length int, sort func([]ds.Comparer) []ds.Comparer, b *testing.B) {
	cb := func(b *testing.B) {
		a := ds.RandomIntegers(length, length)
		b.ResetTimer()
		for i := 0; i < b.N; i++ {
			benchResult = sort(a)
		}
	}
	b.Run(fmt.Sprintf("Benchmark%s%d", name, length), cb)
}