Commit 6262192c authored by John Potocny's avatar John Potocny Committed by Ian Lance Taylor

strings: Add benchmark test for trim function

The strings.Trim function and variants allocate memory on the heap when creating a function to pass into TrimFunc.
Add a benchmark to document the behavior; an issue will be submitted to address this behavior in the compiler if possible.

Change-Id: I8b66721f077951f7e7b8cf3cf346fac27a9b68c0
Reviewed-on: https://go-review.googlesource.com/8200Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent c45751e8
......@@ -569,6 +569,35 @@ func TestTrim(t *testing.T) {
}
}
func BenchmarkTrim(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, tc := range trimTests {
name := tc.f
var f func(string, string) string
switch name {
case "Trim":
f = Trim
case "TrimLeft":
f = TrimLeft
case "TrimRight":
f = TrimRight
case "TrimPrefix":
f = TrimPrefix
case "TrimSuffix":
f = TrimSuffix
default:
b.Errorf("Undefined trim function %s", name)
}
actual := f(tc.in, tc.arg)
if actual != tc.out {
b.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual, tc.out)
}
}
}
}
type predicate struct {
f func(rune) bool
name string
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment