Here is the code I'm testing, I was expecting to see that when bench marking, the pointer based addDataPointer would perform faster than the addData value based function. Why is there no significant changes in performance between the two?
package main
import "fmt"
type BigStruct struct {
name string
data []byte
}
func addData(s BigStruct) BigStruct {
s.data = append([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}, s.data...)
return BigStruct{name: s.name, data: s.data}
}
func (s *BigStruct) addDataPointer() {
s.data = append([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}, s.data...)
}
func main() {
a := BigStruct{name: "greg", data: []byte("abc")}
b := &BigStruct{name: "greg", data: []byte("abc")}
fmt.Println(addData(a))
b.addDataPointer()
fmt.Println(*b)
}
func BenchmarkBigLenPointer(b *testing.B) {
for i := 0; i < b.N; i++ {
big := &BigStruct{name: "greg", data: []byte(strings.Repeat("test1234", 1024))}
big.addDataPointer()
}
}
func BenchmarkBigLen(b *testing.B) {
for i := 0; i < b.N; i++ {
big := BigStruct{name: "greg", data: []byte(strings.Repeat("test1234", 1024))}
addData(big)
}