Skip to content

Instantly share code, notes, and snippets.

@ORBAT
Last active January 14, 2021 23:09
Show Gist options
  • Save ORBAT/8414c2fd626e62027859052f26646026 to your computer and use it in GitHub Desktop.
Save ORBAT/8414c2fd626e62027859052f26646026 to your computer and use it in GitHub Desktop.
Named string error benchmark
package errstr
import (
"errors"
"testing"
)
type ErrString string
func (es ErrString) Error() string {
return string(es)
}
func String(msg string) error {
return ErrString(msg)
}
type errPtrString string
func (e *errPtrString) Error() string {
return string(*e)
}
func newErrPtrStr(msg string) error {
return (*errPtrString)(&msg)
}
const comparisonStr = ErrString("some error")
var (
comparisonStd = errors.New("some error")
comparisonPtrStr = newErrPtrStr("some error")
)
//go:noinline
func givesStringError() error {
return comparisonStr
}
//go:noinline
func givesPtrStringError() error {
return comparisonPtrStr
}
//go:noinline
func givesStdError() error {
return comparisonStd
}
func BenchmarkString(b *testing.B) {
b.Run("== String", func(b *testing.B) {
var err error = givesStringError()
b.ResetTimer()
for i := 0; i < b.N; i++ {
compResult = err == comparisonStr
}
})
b.Run("== std", func(b *testing.B) {
var err error = givesStdError()
b.ResetTimer()
for i := 0; i < b.N; i++ {
compResult = err == comparisonStd
}
})
b.Run("errors.Is String", func(b *testing.B) {
var err error = givesStringError()
b.ResetTimer()
for i := 0; i < b.N; i++ {
compResult = errors.Is(err, comparisonStr)
}
})
b.Run("errors.Is errPtrString", func(b *testing.B) {
var err error = givesPtrStringError()
b.ResetTimer()
for i := 0; i < b.N; i++ {
compResult = errors.Is(err, comparisonPtrStr)
}
})
b.Run("errors.Is std", func(b *testing.B) {
var err error = givesStdError()
b.ResetTimer()
for i := 0; i < b.N; i++ {
compResult = errors.Is(err, comparisonStd)
}
})
b.Run("Error method, String", func(b *testing.B) {
var err error = givesStringError()
b.ResetTimer()
for i := 0; i < b.N; i++ {
errorResult = err.Error()
}
})
b.Run("Error method, errPtrString", func(b *testing.B) {
var err error = givesPtrStringError()
b.ResetTimer()
for i := 0; i < b.N; i++ {
errorResult = err.Error()
}
})
b.Run("Error method, std", func(b *testing.B) {
var err error = givesStdError()
b.ResetTimer()
for i := 0; i < b.N; i++ {
errorResult = err.Error()
}
})
}
var compResult bool
var errorResult string
@ORBAT
Copy link
Author

ORBAT commented Jan 14, 2021

goos: darwin
goarch: amd64

BenchmarkString
BenchmarkString/==_String-12         	983822860	         1.05 ns/op
BenchmarkString/==_std-12            	445498966	         2.72 ns/op
BenchmarkString/errors.Is_String-12  	127660197	         9.54 ns/op
BenchmarkString/errors.Is_errPtrString-12         	174831999	         6.73 ns/op
BenchmarkString/errors.Is_std-12                  	177377359	         6.71 ns/op
BenchmarkString/Error_method,_String-12           	480092460	         2.46 ns/op
BenchmarkString/Error_method,_errPtrString-12     	574903143	         2.13 ns/op
BenchmarkString/Error_method,_std-12              	571575463	         2.12 ns/op

@ORBAT
Copy link
Author

ORBAT commented Jan 14, 2021

go test -bench=. -count=1 -gcflags '-N -l'

goos: darwin
goarch: amd64

BenchmarkString/==_String-12         	126991862	         9.45 ns/op
BenchmarkString/==_std-12            	288934968	         4.10 ns/op
BenchmarkString/errors.Is_String-12  	100000000	        10.6 ns/op
BenchmarkString/errors.Is_errPtrString-12         	169028432	         7.09 ns/op
BenchmarkString/errors.Is_std-12                  	169673967	         7.11 ns/op
BenchmarkString/Error_method,_String-12           	227530143	         5.27 ns/op
BenchmarkString/Error_method,_errPtrString-12     	292550673	         4.10 ns/op
BenchmarkString/Error_method,_std-12              	380578506	         3.25 ns/op

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment