Skip to content

Instantly share code, notes, and snippets.

@ukautz
Created July 11, 2019 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ukautz/605360873145acfe0d9717a9d630c009 to your computer and use it in GitHub Desktop.
Save ukautz/605360873145acfe0d9717a9d630c009 to your computer and use it in GitHub Desktop.
Does test.benchmem work with CGO?
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)
}
}
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)
}
}
$ 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