Skip to content

Instantly share code, notes, and snippets.

@rubensayshi
Last active September 2, 2022 10:09
Show Gist options
  • Save rubensayshi/4f15f3e4a220feaf070636175531ee87 to your computer and use it in GitHub Desktop.
Save rubensayshi/4f15f3e4a220feaf070636175531ee87 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"testing"
)
type SampleStruct struct {
a int
b int
c int
d int
e int
f int
g int
h int
i int
j int
}
func structP(s *SampleStruct) int {
s.a++
c := s.c
return s.a + s.b + c
}
func structV(s SampleStruct) int {
s.a++
c := s.c
return s.a + s.b + c
}
func BenchmarkStructPointerReceiver(b *testing.B) {
r := 0
for i := 0; i < b.N; i++ {
s := &SampleStruct{
a: 1,
b: 1,
c: 1,
d: 1,
e: 1,
f: 1,
g: 1,
h: 1,
i: 1,
j: 1,
}
r = structP(s)
}
fmt.Printf("r: %d \n", r)
}
func BenchmarkStructValueReceiver(b *testing.B) {
r := 0
for i := 0; i < b.N; i++ {
s := SampleStruct{
a: 1,
b: 1,
c: 1,
d: 1,
e: 1,
f: 1,
g: 1,
h: 1,
i: 1,
j: 1,
}
r = structV(s)
}
fmt.Printf("r: %d \n", r)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment