test: add a rudimentary benchmark

pull/21/head
Dominique Merle 3 years ago
parent 563b017080
commit de3098bfe2

@ -0,0 +1,9 @@
> Very rudimentary, for now. Add more!
You're welcome to upload your results with your CPU, especially if they differ from ours.
## Benchmarking
go test benchmark/*.go -bench=.

@ -0,0 +1,79 @@
package benchmark
// from https://github.com/piersy/iterate-slice-bench-go
// with fixes (methink)
import (
"testing"
)
var Val = uint64(42)
var slice = make([]uint64, 500*500)
func BenchmarkRangeReadSliceByIndex(b *testing.B) {
for i := 0; i < b.N; i++ {
for j, _ := range slice {
Val = slice[j] + 1
}
}
}
func BenchmarkRangeReadSliceByValue(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, v := range slice {
Val = v + 1
}
}
}
func BenchmarkRangeWriteSlice(b *testing.B) {
for i := 0; i < b.N; i++ {
for j, _ := range slice {
slice[j] = Val + 1
}
}
}
func BenchmarkRangeReadAndWriteSlice(b *testing.B) {
for i := 0; i < b.N; i++ {
for j, v := range slice {
slice[j] = v + 1
}
}
}
func BenchmarkForIterReadSlice(b *testing.B) {
for i := 0; i < b.N; i++ {
l := len(slice)
for j := 0; j < l; j++ {
Val = slice[j] + 1
}
}
}
func BenchmarkForIterWriteSlice(b *testing.B) {
for i := 0; i < b.N; i++ {
l := len(slice)
for j := 0; j < l; j++ {
slice[j] = Val + 1
}
}
}
func BenchmarkForIterReadAndWriteSlice(b *testing.B) {
for i := 0; i < b.N; i++ {
l := len(slice)
for j := 0; j < l; j++ {
slice[j] = slice[j] + 1
}
}
}
func BenchmarkDecreasingForIterReadAndWriteSlice(b *testing.B) {
for i := 0; i < b.N; i++ {
l := len(slice)
for j := l - 1; j >= 0; j-- {
slice[j] = slice[j] + 1
}
}
}

@ -0,0 +1,13 @@
goos: linux
goarch: amd64
cpu: Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
BenchmarkRangeReadSliceByIndex-8 8845 128883 ns/op
BenchmarkRangeReadSliceByValue-8 14170 86845 ns/op
BenchmarkRangeWriteSlice-8 8294 149656 ns/op
BenchmarkRangeReadAndWriteSlice-8 7885 154031 ns/op
BenchmarkForIterReadSlice-8 9285 131403 ns/op
BenchmarkForIterWriteSlice-8 8377 151931 ns/op
BenchmarkForIterReadAndWriteSlice-8 8504 141878 ns/op
BenchmarkDecreasingForIterReadAndWriteSlice-8 7491 152724 ns/op
PASS
ok command-line-arguments 10.636s
Loading…
Cancel
Save