Created
June 25, 2018 13:53
-
-
Save packrat386/b49ca68e0be4e84fc2575818ae02ad2d to your computer and use it in GitHub Desktop.
fmt.Errorf might not be as bad as I thought
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 main | |
import ( | |
"errors" | |
"fmt" | |
"testing" | |
) | |
var err error | |
func BenchmarkErrorsNew(b *testing.B) { | |
var e error | |
for n := 0; n < b.N; n++ { | |
e = errors.New("ruh roh") | |
} | |
err = e | |
} | |
func BenchmarkFmtErrorf(b *testing.B) { | |
var e error | |
for n := 0; n < b.N; n++ { | |
e = fmt.Errorf("ruh roh") | |
} | |
err = e | |
} |
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 main | |
import ( | |
"errors" | |
"fmt" | |
"testing" | |
) | |
func BenchmarkErrorsNew(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
_ = errors.New("ruh roh") | |
} | |
} | |
func BenchmarkFmtErrorf(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
_ = fmt.Errorf("ruh roh") | |
} | |
} |
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
[fg-386] ~ > go test -bench=. -benchmem=true errors_test.go | |
goos: darwin | |
goarch: amd64 | |
BenchmarkErrorsNew-8 50000000 24.6 ns/op 16 B/op 1 allocs/op | |
BenchmarkFmtErrorf-8 20000000 80.4 ns/op 24 B/op 2 allocs/op | |
PASS | |
ok command-line-arguments 2.982s | |
[fg-386] ~ > go test -bench=. -benchmem=true errors_wrong_test.go | |
goos: darwin | |
goarch: amd64 | |
BenchmarkErrorsNew-8 2000000000 0.29 ns/op 0 B/op 0 allocs/op | |
BenchmarkFmtErrorf-8 20000000 82.2 ns/op 24 B/op 2 allocs/op | |
PASS | |
ok command-line-arguments 2.369s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment