Answer the question
In order to leave comments, you need to log in
Is it possible to get rid of allocation in this case?
package test1
import (
"testing"
)
type Transport interface {
Data() map[interface{}]string
}
type User struct {
Name string
Age int
Year time.Time
}
func (user *User) Data() map[interface{}]string {
return map[interface{}]string{
&user.Name: "somedata",
&user.Age: "somedata",
&user.Year: "somedata",
}
}
func BenchmarkTransport(b *testing.B) {
var t Transport = &User{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for k, v := range t.Data() {
_ = k
_ = v
}
}
}
func BenchmarkWithout(b *testing.B) {
t := &User{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for k, v := range t.Data() {
_ = k
_ = v
}
}
}
В BenchmarkTransport 2 аллокации в методе Data return map[interface{}]string{, в во 2 бенчмарке 0. Можно ли как то избавится от аллокаций в данном случае?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question