]> Cypherpunks repositories - gostls13.git/commitdiff
bytes, strings: add Fields benchmarks
authorRuss Cox <rsc@golang.org>
Tue, 18 Sep 2012 19:02:08 +0000 (15:02 -0400)
committerRuss Cox <rsc@golang.org>
Tue, 18 Sep 2012 19:02:08 +0000 (15:02 -0400)
The performance changes will be a few different CLs.
Start with benchmarks as a baseline.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/6537043

src/pkg/bytes/bytes_test.go
src/pkg/strings/strings_test.go

index 0e2ef504cfc599a3d0767d7d64bc3715049d14f7..124760ac741d0bba559490177f6c903c32585d4d 100644 (file)
@@ -6,6 +6,7 @@ package bytes_test
 
 import (
        . "bytes"
+       "math/rand"
        "reflect"
        "testing"
        "unicode"
@@ -567,6 +568,14 @@ func TestFields(t *testing.T) {
 }
 
 func TestFieldsFunc(t *testing.T) {
+       for _, tt := range fieldstests {
+               a := FieldsFunc([]byte(tt.s), unicode.IsSpace)
+               result := arrayOfString(a)
+               if !eq(result, tt.a) {
+                       t.Errorf("FieldsFunc(%q, unicode.IsSpace) = %v; want %v", tt.s, a, tt.a)
+                       continue
+               }
+       }
        pred := func(c rune) bool { return c == 'X' }
        var fieldsFuncTests = []FieldsTest{
                {"", []string{}},
@@ -1014,3 +1023,39 @@ func TestEqualFold(t *testing.T) {
                }
        }
 }
+
+var makeFieldsInput = func() []byte {
+       x := make([]byte, 1<<20)
+       // Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space. 
+       for i := range x {
+               switch rand.Intn(10) {
+               case 0:
+                       x[i] = ' '
+               case 1:
+                       if i > 0 && x[i-1] == 'x' {
+                               copy(x[i-1:], "χ")
+                               break
+                       }
+                       fallthrough
+               default:
+                       x[i] = 'x'
+               }
+       }
+       return x
+}
+
+var fieldsInput = makeFieldsInput()
+
+func BenchmarkFields(b *testing.B) {
+       b.SetBytes(int64(len(fieldsInput)))
+       for i := 0; i < b.N; i++ {
+               Fields(fieldsInput)
+       }
+}
+
+func BenchmarkFieldsFunc(b *testing.B) {
+       b.SetBytes(int64(len(fieldsInput)))
+       for i := 0; i < b.N; i++ {
+               FieldsFunc(fieldsInput, unicode.IsSpace)
+       }
+}
index 54046d68aa0073dea15d6f1cc47e44d6cb8d36dc..c271e48ab249bd463259f313aec0f538809433fa 100644 (file)
@@ -7,6 +7,7 @@ package strings_test
 import (
        "bytes"
        "io"
+       "math/rand"
        "reflect"
        . "strings"
        "testing"
@@ -311,6 +312,13 @@ var FieldsFuncTests = []FieldsTest{
 }
 
 func TestFieldsFunc(t *testing.T) {
+       for _, tt := range fieldstests {
+               a := FieldsFunc(tt.s, unicode.IsSpace)
+               if !eq(a, tt.a) {
+                       t.Errorf("FieldsFunc(%q, unicode.IsSpace) = %v; want %v", tt.s, a, tt.a)
+                       continue
+               }
+       }
        pred := func(c rune) bool { return c == 'X' }
        for _, tt := range FieldsFuncTests {
                a := FieldsFunc(tt.s, pred)
@@ -984,3 +992,39 @@ func TestEqualFold(t *testing.T) {
                }
        }
 }
+
+var makeFieldsInput = func() string {
+       x := make([]byte, 1<<20)
+       // Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space. 
+       for i := range x {
+               switch rand.Intn(10) {
+               case 0:
+                       x[i] = ' '
+               case 1:
+                       if i > 0 && x[i-1] == 'x' {
+                               copy(x[i-1:], "χ")
+                               break
+                       }
+                       fallthrough
+               default:
+                       x[i] = 'x'
+               }
+       }
+       return string(x)
+}
+
+var fieldsInput = makeFieldsInput()
+
+func BenchmarkFields(b *testing.B) {
+       b.SetBytes(int64(len(fieldsInput)))
+       for i := 0; i < b.N; i++ {
+               Fields(fieldsInput)
+       }
+}
+
+func BenchmarkFieldsFunc(b *testing.B) {
+       b.SetBytes(int64(len(fieldsInput)))
+       for i := 0; i < b.N; i++ {
+               FieldsFunc(fieldsInput, unicode.IsSpace)
+       }
+}