diff options
Diffstat (limited to 'libgo/go/runtime/string_test.go')
-rw-r--r-- | libgo/go/runtime/string_test.go | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/libgo/go/runtime/string_test.go b/libgo/go/runtime/string_test.go index 555a7fc7ef9..03327bbb1e2 100644 --- a/libgo/go/runtime/string_test.go +++ b/libgo/go/runtime/string_test.go @@ -9,6 +9,7 @@ import ( "strconv" "strings" "testing" + "unicode/utf8" ) // Strings and slices that don't escape and fit into tmpBuf are stack allocated, @@ -110,6 +111,43 @@ var stringdata = []struct{ name, data string }{ {"MixedLength", "$Ѐࠀက퀀𐀀\U00040000\U0010FFFF"}, } +var sinkInt int + +func BenchmarkRuneCount(b *testing.B) { + // Each sub-benchmark counts the runes in a string in a different way. + b.Run("lenruneslice", func(b *testing.B) { + for _, sd := range stringdata { + b.Run(sd.name, func(b *testing.B) { + for i := 0; i < b.N; i++ { + sinkInt += len([]rune(sd.data)) + } + }) + } + }) + b.Run("rangeloop", func(b *testing.B) { + for _, sd := range stringdata { + b.Run(sd.name, func(b *testing.B) { + for i := 0; i < b.N; i++ { + n := 0 + for range sd.data { + n++ + } + sinkInt += n + } + }) + } + }) + b.Run("utf8.RuneCountInString", func(b *testing.B) { + for _, sd := range stringdata { + b.Run(sd.name, func(b *testing.B) { + for i := 0; i < b.N; i++ { + sinkInt += utf8.RuneCountInString(sd.data) + } + }) + } + }) +} + func BenchmarkRuneIterate(b *testing.B) { b.Run("range", func(b *testing.B) { for _, sd := range stringdata { @@ -125,7 +163,7 @@ func BenchmarkRuneIterate(b *testing.B) { for _, sd := range stringdata { b.Run(sd.name, func(b *testing.B) { for i := 0; i < b.N; i++ { - for _ = range sd.data { + for range sd.data { } } }) @@ -135,7 +173,7 @@ func BenchmarkRuneIterate(b *testing.B) { for _, sd := range stringdata { b.Run(sd.name, func(b *testing.B) { for i := 0; i < b.N; i++ { - for _, _ = range sd.data { + for range sd.data { } } }) |