Last active
January 14, 2021 23:09
-
-
Save ORBAT/8414c2fd626e62027859052f26646026 to your computer and use it in GitHub Desktop.
Named string error benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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