Skip to content

Instantly share code, notes, and snippets.

@yurishkuro
Created August 5, 2017 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yurishkuro/b135e6750df0f95aa86c35a77cc736d4 to your computer and use it in GitHub Desktop.
Save yurishkuro/b135e6750df0f95aa86c35a77cc736d4 to your computer and use it in GitHub Desktop.
by-value vs. by-pointer benchmark
package jaeger
// go test -bench=.
// BenchmarkByValue-8 2000000000 0.56 ns/op
// BenchmarkByPointer-8 2000000000 0.28 ns/op
import "testing"
func BenchmarkByValue(b *testing.B) {
b1 := b1{}
for n := 0; n < b.N; n++ {
b1.setBaggage("x", "y")
}
}
func BenchmarkByPointer(b *testing.B) {
b2 := &b2{}
for n := 0; n < b.N; n++ {
b2.setBaggage("x", "y")
}
}
type b1 struct {
logger Logger
metrics *Metrics
}
func (b b1) setBaggage(k, v string) {
b.logFields(k, v)
}
func (b b1) logFields(k, v string) {
// no-op
}
type b2 struct {
logger Logger
metrics *Metrics
}
func (b *b2) setBaggage(k, v string) {
b.logFields(k, v)
}
func (b *b2) logFields(k, v string) {
// no-op
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment