diff options
Diffstat (limited to 'src/pkg')
| -rw-r--r-- | src/pkg/runtime/alg.c | 4 | ||||
| -rw-r--r-- | src/pkg/runtime/string.goc | 20 | ||||
| -rw-r--r-- | src/pkg/runtime/string_test.go | 45 |
3 files changed, 69 insertions, 0 deletions
diff --git a/src/pkg/runtime/alg.c b/src/pkg/runtime/alg.c index bc848da38c..ce872755ff 100644 --- a/src/pkg/runtime/alg.c +++ b/src/pkg/runtime/alg.c @@ -324,6 +324,10 @@ runtime·strequal(bool *eq, uintptr s, void *a, void *b) *eq = false; return; } + if(((String*)a)->str == ((String*)b)->str) { + *eq = true; + return; + } runtime·memequal(eq, alen, ((String*)a)->str, ((String*)b)->str); } diff --git a/src/pkg/runtime/string.goc b/src/pkg/runtime/string.goc index 7cab6d2417..b72a1aa5e7 100644 --- a/src/pkg/runtime/string.goc +++ b/src/pkg/runtime/string.goc @@ -204,6 +204,26 @@ func cmpstring(s1 String, s2 String) (v int32) { v = cmpstring(s1, s2); } +func eqstring(s1 String, s2 String) (v bool) { + uint32 i, l; + + if(s1.len != s2.len) { + v = false; + return; + } + if(s1.str == s2.str) { + v = true; + return; + } + l = s1.len; + for(i=0; i<l; i++) + if(s1.str[i] != s2.str[i]) { + v = false; + return; + } + v = true; +} + int32 runtime·strcmp(byte *s1, byte *s2) { diff --git a/src/pkg/runtime/string_test.go b/src/pkg/runtime/string_test.go new file mode 100644 index 0000000000..8f13f0f428 --- /dev/null +++ b/src/pkg/runtime/string_test.go @@ -0,0 +1,45 @@ +package runtime_test + +import ( + "testing" +) + +func BenchmarkCompareStringEqual(b *testing.B) { + bytes := []byte("Hello Gophers!") + s1, s2 := string(bytes), string(bytes) + for i := 0; i < b.N; i++ { + if s1 != s2 { + b.Fatal("s1 != s2") + } + } +} + +func BenchmarkCompareStringIdentical(b *testing.B) { + s1 := "Hello Gophers!" + s2 := s1 + for i := 0; i < b.N; i++ { + if s1 != s2 { + b.Fatal("s1 != s2") + } + } +} + +func BenchmarkCompareStringSameLength(b *testing.B) { + s1 := "Hello Gophers!" + s2 := "Hello, Gophers" + for i := 0; i < b.N; i++ { + if s1 == s2 { + b.Fatal("s1 == s2") + } + } +} + +func BenchmarkCompareStringDifferentLength(b *testing.B) { + s1 := "Hello Gophers!" + s2 := "Hello, Gophers!" + for i := 0; i < b.N; i++ { + if s1 == s2 { + b.Fatal("s1 == s2") + } + } +} |
