Created
July 11, 2019 14:05
-
-
Save ukautz/605360873145acfe0d9717a9d630c009 to your computer and use it in GitHub Desktop.
Does test.benchmem work with CGO?
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 foo | |
import ( | |
"C" | |
"strings" | |
) | |
var testString = strings.Repeat("x", 1024) | |
func NativeStrings(amount int) { | |
strs := make([]string, amount) | |
for i := range strs { | |
strs[i] = ""+testString | |
} | |
} | |
func CGOStrings(amount int) { | |
strs := make([]*C.char, amount) | |
for i := range strs { | |
strs[i] = C.CString(""+testString) | |
} | |
} |
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 foo | |
import ( | |
"testing" | |
) | |
const ( | |
many = 10000 | |
few = 100 | |
) | |
func BenchmarkNativeStringsMany(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
NativeStrings(many) | |
} | |
} | |
func BenchmarkNativeStringsFew(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
NativeStrings(few) | |
} | |
} | |
func BenchmarkCGOStringsMany(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
CGOStrings(many) | |
} | |
} | |
func BenchmarkCGOStringsFew(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
CGOStrings(few) | |
} | |
} |
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
$ go test -bench=. -benchmem | |
goos: linux | |
goarch: amd64 | |
pkg: go-cgo-mem | |
BenchmarkNativeStringsMany-4 10000 221103 ns/op 163840 B/op 1 allocs/op | |
BenchmarkNativeStringsFew-4 1000000 2027 ns/op 1792 B/op 1 allocs/op | |
BenchmarkCGOStringsMany-4 300 4692834 ns/op 81920 B/op 1 allocs/op | |
BenchmarkCGOStringsFew-4 30000 56950 ns/op 896 B/op 1 allocs/op | |
PASS | |
ok go-cgo-mem 8.736s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment