Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
benchmark error loops
package main
import (
"errors"
)
var Err = errors.New("test err")
func addOne(y, x int) (int, error) {
if x%y == 0 {
return x, Err
}
return x + 1, nil
}
func errSeq5(a, b int) (i int, err error) {
i = a
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
return
}
func errLoop5(a, b int) (i int, err error) {
i = a
steps:
for jump := 1; err == nil; jump++ {
switch jump - 1 {
default:
break steps
case 0:
i, err = addOne(b, i)
case 1:
i, err = addOne(b, i)
case 2:
i, err = addOne(b, i)
case 3:
i, err = addOne(b, i)
case 4:
i, err = addOne(b, i)
}
}
return i, err
}
func errSeq10(a, b int) (i int, err error) {
i = a
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
return
}
func errLoop10(a, b int) (i int, err error) {
i = a
steps:
for jump := 1; err == nil; jump++ {
switch jump - 1 {
default:
break steps
case 0:
i, err = addOne(b, i)
case 1:
i, err = addOne(b, i)
case 2:
i, err = addOne(b, i)
case 3:
i, err = addOne(b, i)
case 4:
i, err = addOne(b, i)
case 5:
i, err = addOne(b, i)
case 6:
i, err = addOne(b, i)
case 7:
i, err = addOne(b, i)
case 8:
i, err = addOne(b, i)
case 9:
i, err = addOne(b, i)
}
}
return i, err
}
func errSeq20(a, b int) (i int, err error) {
i = a
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
if err != nil {
return
}
i, err = addOne(b, i)
return
}
func errLoop20(a, b int) (i int, err error) {
i = a
steps:
for jump := 1; err == nil; jump++ {
switch jump - 1 {
default:
break steps
case 0:
i, err = addOne(b, i)
case 1:
i, err = addOne(b, i)
case 2:
i, err = addOne(b, i)
case 3:
i, err = addOne(b, i)
case 4:
i, err = addOne(b, i)
case 5:
i, err = addOne(b, i)
case 6:
i, err = addOne(b, i)
case 7:
i, err = addOne(b, i)
case 8:
i, err = addOne(b, i)
case 9:
i, err = addOne(b, i)
case 10:
i, err = addOne(b, i)
case 11:
i, err = addOne(b, i)
case 12:
i, err = addOne(b, i)
case 13:
i, err = addOne(b, i)
case 14:
i, err = addOne(b, i)
case 15:
i, err = addOne(b, i)
case 16:
i, err = addOne(b, i)
case 17:
i, err = addOne(b, i)
case 18:
i, err = addOne(b, i)
case 19:
i, err = addOne(b, i)
}
}
return i, err
}
package main
import (
"testing"
)
/*
Results:
PASS
BenchmarkErrLoop5 20000000 69.5 ns/op +18%
BenchmarkErrBench5 30000000 59.0 ns/op
BenchmarkErrLoop10 20000000 115.0 ns/op +36%
BenchmarkErrBench10 20000000 84.6 ns/op
BenchmarkErrLoop20 10000000 164.0 ns/op +27%
BenchmarkErrBench20 10000000 129.0 ns/op
ok github.com/metakeule/try/bencherr 10.745s
*/
func TestErrLoop(t *testing.T) {
var expected = 5
num, _ := errLoop5(1, 5)
if num != expected {
t.Errorf("expected: %d, got %d", expected, num)
}
}
var x bool
func BenchmarkErrLoop5(b *testing.B) {
b.StopTimer()
var nums int
var expected = 5350
b.StartTimer()
for i := 0; i < b.N; i++ {
add, _ := errLoop5(i+2, 5)
nums += add
}
x = nums != expected
}
func BenchmarkErrBench5(b *testing.B) {
b.StopTimer()
var nums int
var expected = 5350
b.StartTimer()
for i := 0; i < b.N; i++ {
add, _ := errSeq5(i+2, 5)
nums += add
}
x = nums != expected
}
func BenchmarkErrLoop10(b *testing.B) {
b.StopTimer()
var nums int
var expected = 5350
b.StartTimer()
for i := 0; i < b.N; i++ {
add, _ := errLoop10(i+2, 9)
nums += add
}
x = nums != expected
}
func BenchmarkErrBench10(b *testing.B) {
b.StopTimer()
var nums int
var expected = 5350
b.StartTimer()
for i := 0; i < b.N; i++ {
add, _ := errSeq10(i+2, 8)
nums += add
}
x = nums != expected
}
func BenchmarkErrLoop20(b *testing.B) {
b.StopTimer()
var nums int
var expected = 5350
b.StartTimer()
for i := 0; i < b.N; i++ {
add, _ := errLoop20(i+2, 13)
nums += add
}
x = nums != expected
}
func BenchmarkErrBench20(b *testing.B) {
b.StopTimer()
var nums int
var expected = 5350
b.StartTimer()
for i := 0; i < b.N; i++ {
add, _ := errSeq20(i+2, 13)
nums += add
}
x = nums != expected
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment