Skip to content

Instantly share code, notes, and snippets.

@embano1
Last active February 18, 2020 19:12
Show Gist options
  • Save embano1/44d7d66ace1dec171f17f44fd3af2b4d to your computer and use it in GitHub Desktop.
Save embano1/44d7d66ace1dec171f17f44fd3af2b4d to your computer and use it in GitHub Desktop.
Benchmark fmt.Sprintf %v vs %s
go test -bench=. -benchmem                    
goos: darwin
goarch: amd64
pkg: fmt-perf
BenchmarkFmtV-16         6899274               174 ns/op              64 B/op          1 allocs/op
BenchmarkFmtS-16         9181304               129 ns/op              80 B/op          2 allocs/op
PASS
ok      fmt-perf        4.138s
package fmtperf
import (
"errors"
"fmt"
"testing"
)
var (
err = errors.New("failed to connect to sidecar server")
output string
)
func BenchmarkFmtV(b *testing.B) {
for i := 0; i < b.N; i++ {
msg := fmt.Sprintf("could not get root node: %v", err)
output = msg
}
}
func BenchmarkFmtS(b *testing.B) {
for i := 0; i < b.N; i++ {
msg := fmt.Sprintf("could not get root node: %s", err.Error())
output = msg
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment