Created
August 5, 2017 00:08
-
-
Save yurishkuro/b135e6750df0f95aa86c35a77cc736d4 to your computer and use it in GitHub Desktop.
by-value vs. by-pointer 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 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