Last active
September 2, 2022 10:09
-
-
Save rubensayshi/4f15f3e4a220feaf070636175531ee87 to your computer and use it in GitHub Desktop.
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 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