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.
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
++ {
21 func BenchmarkCompareStringIdentical(b
*testing
.B
) {
22 s1
:= "Hello Gophers!"
24 for i
:= 0; i
< b
.N
; i
++ {
31 func BenchmarkCompareStringSameLength(b
*testing
.B
) {
32 s1
:= "Hello Gophers!"
33 s2
:= "Hello, Gophers"
34 for i
:= 0; i
< b
.N
; i
++ {
41 func BenchmarkCompareStringDifferentLength(b
*testing
.B
) {
42 s1
:= "Hello Gophers!"
43 s2
:= "Hello, Gophers!"
44 for i
:= 0; i
< b
.N
; i
++ {
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!"...)
56 s1
, s2
:= string(bytes
), "hello"+string(bytes
)
57 for i
:= 0; i
< b
.N
; i
++ {
58 if s1
!= s2
[len("hello"):] {
62 b
.SetBytes(int64(len(s1
)))
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!"...)
70 s1
, s2
:= string(bytes
), string(bytes
)
71 for i
:= 0; i
< b
.N
; i
++ {
76 b
.SetBytes(int64(len(s1
)))