Created
February 11, 2022 06:45
-
-
Save honmaple/b28177cb31c288593773df4983e7a5ba to your computer and use it in GitHub Desktop.
test function slice param
This file contains hidden or 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 ( | |
"testing" | |
) | |
type myStruct struct { | |
list []string | |
} | |
func BenchmarkSliceNoPointer(b *testing.B) { | |
b.ReportAllocs() | |
slice := make([]string, 1000) | |
for i := 0; i < b.N; i++ { | |
testNoPointer(slice) | |
} | |
} | |
func BenchmarkSlicePointer(b *testing.B) { | |
b.ReportAllocs() | |
slice := make([]string, 1000) | |
for i := 0; i < b.N; i++ { | |
testPointer(&slice) | |
} | |
} | |
func BenchmarkSliceWithStruct(b *testing.B) { | |
b.ReportAllocs() | |
slice := make([]string, 1000) | |
s := &myStruct{list: slice} | |
for i := 0; i < b.N; i++ { | |
testPointerWithStruct(s) | |
} | |
} | |
func testNoPointer(list []string) { | |
for i := 0; i < 100; i++ { | |
list[i] = "test string" | |
} | |
} | |
func testPointer(list *([]string)) { | |
for i := 0; i < 100; i++ { | |
(*list)[i] = "test string" | |
} | |
} | |
func testPointerWithStruct(s *myStruct) { | |
for i := 0; i < 100; i++ { | |
s.list[i] = "test string" | |
} | |
} |
Author
honmaple
commented
Feb 11, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment