-
Notifications
You must be signed in to change notification settings - Fork 15
/
benchmark.go
62 lines (52 loc) · 1.62 KB
/
benchmark.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"fmt"
"reflect"
"runtime"
"sync"
"time"
)
const (
// The range of values for n passed to the individual benchmarks
start, end, step int = 100000, 3000000, 100000
)
var (
wg sync.WaitGroup
testByteString = []byte(fmt.Sprint("test value"))
test10Kilobyte = make([]byte, 10240)
)
// timeTrack will print out the number of nanoseconds since the start time divided by n
// Useful for printing out how long each iteration took in a benchmark
func timeTrack(start time.Time, n int) {
loopNS := time.Since(start).Nanoseconds() / int64(n)
fmt.Print(loopNS)
}
// iterations is used to print out the CSV header with iteration counts
func iterations(n int) {
fmt.Print(n)
}
// funcName returns just the function name of a string, given any function at all
func funcName(f func(int)) string {
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()[5:]
}
// runIterations executes the tests in a loop with the given parameters
func runIterations(name string, start, end, step int, f func(int)) {
fmt.Print(name, ",")
for i := start; i <= end; i += step {
f(i)
fmt.Print(",")
}
fmt.Println()
}
func main() {
// first, print the CSV header with iteration counts
runIterations("iterations", start, end, step, iterations)
allFunctions := append(seanFunctions, zhenjlFunctions...)
allFunctions = append(allFunctions, mtchavezFunctions...)
allFunctions = append(allFunctions, huanduFunctions...)
allFunctions = append(allFunctions, colFunctions...)
allFunctions = append(allFunctions, ryszardFunctions...)
for _, f := range allFunctions {
runIterations(funcName(f), start, end, step, f)
}
}