github.com/rohankumardubey/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/runtime/string_test.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package runtime_test 6 7 import ( 8 "testing" 9 ) 10 11 func BenchmarkCompareStringEqual(b *testing.B) { 12 bytes := []byte("Hello Gophers!") 13 s1, s2 := string(bytes), string(bytes) 14 for i := 0; i < b.N; i++ { 15 if s1 != s2 { 16 b.Fatal("s1 != s2") 17 } 18 } 19 } 20 21 func BenchmarkCompareStringIdentical(b *testing.B) { 22 s1 := "Hello Gophers!" 23 s2 := s1 24 for i := 0; i < b.N; i++ { 25 if s1 != s2 { 26 b.Fatal("s1 != s2") 27 } 28 } 29 } 30 31 func BenchmarkCompareStringSameLength(b *testing.B) { 32 s1 := "Hello Gophers!" 33 s2 := "Hello, Gophers" 34 for i := 0; i < b.N; i++ { 35 if s1 == s2 { 36 b.Fatal("s1 == s2") 37 } 38 } 39 } 40 41 func BenchmarkCompareStringDifferentLength(b *testing.B) { 42 s1 := "Hello Gophers!" 43 s2 := "Hello, Gophers!" 44 for i := 0; i < b.N; i++ { 45 if s1 == s2 { 46 b.Fatal("s1 == s2") 47 } 48 } 49 } 50 51 func BenchmarkCompareStringBigUnaligned(b *testing.B) { 52 bytes := make([]byte, 0, 1<<20) 53 for len(bytes) < 1<<20 { 54 bytes = append(bytes, "Hello Gophers!"...) 55 } 56 s1, s2 := string(bytes), "hello"+string(bytes) 57 for i := 0; i < b.N; i++ { 58 if s1 != s2[len("hello"):] { 59 b.Fatal("s1 != s2") 60 } 61 } 62 b.SetBytes(int64(len(s1))) 63 } 64 65 func BenchmarkCompareStringBig(b *testing.B) { 66 bytes := make([]byte, 0, 1<<20) 67 for len(bytes) < 1<<20 { 68 bytes = append(bytes, "Hello Gophers!"...) 69 } 70 s1, s2 := string(bytes), string(bytes) 71 for i := 0; i < b.N; i++ { 72 if s1 != s2 { 73 b.Fatal("s1 != s2") 74 } 75 } 76 b.SetBytes(int64(len(s1))) 77 }