├── .gitignore ├── README.md └── bench_test.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # strings-vs-bytes 2 | ```bash 3 | BenchmarkBytesToStrings-4 50000000 20.5 ns/op 32 B/op 1 allocs/op 4 | BenchmarkBytesCompareSame-4 300000000 4.44 ns/op 0 B/op 0 allocs/op 5 | BenchmarkBytesCompareDifferent-4 300000000 4.57 ns/op 0 B/op 0 allocs/op 6 | BenchmarkStringsCompareSame-4 2000000000 1.67 ns/op 0 B/op 0 allocs/op 7 | BenchmarkStringsCompareDifferent-4 200000000 8.19 ns/op 0 B/op 0 allocs/op 8 | BenchmarkBytesContains-4 100000000 11.8 ns/op 0 B/op 0 allocs/op 9 | BenchmarkStringsContains-4 100000000 10.7 ns/op 0 B/op 0 allocs/op 10 | BenchmarkBytesIndex-4 200000000 7.11 ns/op 0 B/op 0 allocs/op 11 | BenchmarkStringIndex-4 300000000 6.09 ns/op 0 B/op 0 allocs/op 12 | BenchmarkBytesReplace-4 20000000 60.1 ns/op 32 B/op 1 allocs/op 13 | BenchmarkStringsReplace-4 20000000 91.4 ns/op 64 B/op 2 allocs/op 14 | BenchmarkBytesConcat2-4 50000000 23.9 ns/op 32 B/op 1 allocs/op 15 | BenchmarkStringsConcat2-4 50000000 37.4 ns/op 32 B/op 1 allocs/op 16 | BenchmarkStringsJoin2-4 30000000 48.5 ns/op 32 B/op 1 allocs/op 17 | BenchmarkMapHints-4 200000000 7.08 ns/op 0 B/op 0 allocs/op 18 | BenchmarkMapsHints_Dont-4 100000000 12.2 ns/op 0 B/op 0 allocs/op 19 | ``` 20 | 21 | ### Link : 22 | https://medium.com/@felipedutratine/in-golang-should-i-work-with-bytes-or-strings-8bd1f5a7fd48#.6m2j7ssec 23 | -------------------------------------------------------------------------------- /bench_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "strings" 6 | "testing" 7 | ) 8 | 9 | var result interface{} 10 | 11 | //BenchmarkBytesToStrings convert a []bytes in a string 12 | //https://golang.org/pkg/bytes/#Compare 13 | func BenchmarkBytesToStrings(b *testing.B) { 14 | 15 | s1 := []byte("string to convert") 16 | 17 | b.ResetTimer() 18 | b.ReportAllocs() 19 | var r string 20 | for n := 0; n < b.N; n++ { 21 | r = string(s1) 22 | } 23 | 24 | result = r 25 | } 26 | 27 | //BenchmarkBytesCompareSame compare 2 []bytes. 28 | //https://golang.org/pkg/bytes/#Compare 29 | func BenchmarkBytesCompareSame(b *testing.B) { 30 | 31 | s1 := []byte("string to compare") 32 | s2 := []byte("string to compare") 33 | 34 | b.ResetTimer() 35 | b.ReportAllocs() 36 | var r int 37 | for n := 0; n < b.N; n++ { 38 | r = bytes.Compare(s1, s2) 39 | } 40 | 41 | result = r 42 | } 43 | 44 | //BenchmarkBytesCompareDifferent compare 2 []bytes. 45 | //https://golang.org/pkg/bytes/#Compare 46 | func BenchmarkBytesCompareDifferent(b *testing.B) { 47 | 48 | s1 := []byte("string to compare A") 49 | s2 := []byte("string to compare B") 50 | 51 | b.ResetTimer() 52 | b.ReportAllocs() 53 | var r int 54 | for n := 0; n < b.N; n++ { 55 | r = bytes.Compare(s1, s2) 56 | } 57 | 58 | result = r 59 | } 60 | 61 | //BenchmarkStringsCompareSame compare 2 strings. 62 | //https://golang.org/pkg/strings/#Compare 63 | func BenchmarkStringsCompareSame(b *testing.B) { 64 | 65 | s1 := "string to compare" 66 | s2 := "string to compare" 67 | 68 | b.ResetTimer() 69 | b.ReportAllocs() 70 | var r int 71 | for n := 0; n < b.N; n++ { 72 | r = strings.Compare(s1, s2) 73 | } 74 | 75 | result = r 76 | } 77 | 78 | //BenchmarkStringsCompareDifferent compare 2 strings. 79 | //https://golang.org/pkg/strings/#Compare 80 | func BenchmarkStringsCompareDifferent(b *testing.B) { 81 | 82 | s1 := "string to compare A" 83 | s2 := "string to compare B" 84 | 85 | b.ResetTimer() 86 | b.ReportAllocs() 87 | var r int 88 | for n := 0; n < b.N; n++ { 89 | r = strings.Compare(s1, s2) 90 | } 91 | 92 | result = r 93 | } 94 | 95 | //BenchmarkBytesContains check contains method 96 | //https://golang.org/pkg/bytes/#Contains 97 | func BenchmarkBytesContains(b *testing.B) { 98 | 99 | s1 := []byte("string to compare") 100 | s2 := []byte("comparc") 101 | 102 | b.ResetTimer() 103 | b.ReportAllocs() 104 | var r bool 105 | for n := 0; n < b.N; n++ { 106 | r = bytes.Contains(s1, s2) 107 | } 108 | 109 | result = r 110 | } 111 | 112 | //BenchmarkStringsContains check contains method 113 | //https://golang.org/pkg/strings/#Contains 114 | func BenchmarkStringsContains(b *testing.B) { 115 | 116 | s1 := "string to compare" 117 | s2 := "comparc" 118 | 119 | b.ResetTimer() 120 | b.ReportAllocs() 121 | var r bool 122 | for n := 0; n < b.N; n++ { 123 | r = strings.Contains(s1, s2) 124 | } 125 | 126 | result = r 127 | } 128 | 129 | //BenchmarkBytesIndex check contains index 130 | //https://golang.org/pkg/bytes/#Index 131 | func BenchmarkBytesIndex(b *testing.B) { 132 | 133 | s1 := []byte("string to compare") 134 | s2 := []byte("e") 135 | 136 | b.ResetTimer() 137 | b.ReportAllocs() 138 | var r int 139 | for n := 0; n < b.N; n++ { 140 | r = bytes.Index(s1, s2) 141 | } 142 | 143 | result = r 144 | } 145 | 146 | //BenchmarkStringIndex check contains index 147 | //https://golang.org/pkg/strings/#Index 148 | func BenchmarkStringIndex(b *testing.B) { 149 | 150 | s1 := "string to compare" 151 | s2 := "e" 152 | 153 | b.ResetTimer() 154 | b.ReportAllocs() 155 | var r int 156 | for n := 0; n < b.N; n++ { 157 | r = strings.Index(s1, s2) 158 | } 159 | 160 | result = r 161 | } 162 | 163 | //BenchmarkBytesReplace check replace method 164 | //https://golang.org/pkg/bytes/#Replace 165 | func BenchmarkBytesReplace(b *testing.B) { 166 | 167 | s1 := []byte("string to comparc") 168 | s2 := []byte("comparc") 169 | s3 := []byte("compare") 170 | 171 | b.ResetTimer() 172 | b.ReportAllocs() 173 | var r []byte 174 | for n := 0; n < b.N; n++ { 175 | r = bytes.Replace(s1, s2, s3, -1) 176 | } 177 | 178 | result = r 179 | } 180 | 181 | //BenchmarkStringsReplace check replace method 182 | //https://golang.org/pkg/strings/#Replace 183 | func BenchmarkStringsReplace(b *testing.B) { 184 | 185 | s1 := "string to comparc" 186 | s2 := "comparc" 187 | s3 := "compare" 188 | 189 | b.ResetTimer() 190 | b.ReportAllocs() 191 | var r string 192 | for n := 0; n < b.N; n++ { 193 | r = strings.Replace(s1, s2, s3, -1) 194 | } 195 | 196 | result = r 197 | } 198 | 199 | //BenchmarkBytesConcat concats 2 bytes 200 | func BenchmarkBytesConcat2(b *testing.B) { 201 | 202 | s1 := []byte("string to compare") 203 | s2 := []byte("string to add") 204 | b.ResetTimer() 205 | b.ReportAllocs() 206 | 207 | for n := 0; n < b.N; n++ { 208 | r := make([]byte, 0, len(s1)+len(s2)) 209 | r = append(r, s1...) 210 | r = append(r, s2...) 211 | } 212 | } 213 | 214 | //BenchmarkStringsConcat concats 2 strings 215 | func BenchmarkStringsConcat2(b *testing.B) { 216 | 217 | s1 := "string to compare" 218 | s2 := "string to add" 219 | b.ResetTimer() 220 | b.ReportAllocs() 221 | var r string 222 | for n := 0; n < b.N; n++ { 223 | r = s1 + s2 224 | } 225 | 226 | result = r 227 | } 228 | 229 | //BenchmarkStringsJoin joins 2 strings 230 | //https://golang.org/pkg/strings/#Join 231 | func BenchmarkStringsJoin2(b *testing.B) { 232 | 233 | s1 := "string to compare" 234 | s2 := "string to add" 235 | b.ResetTimer() 236 | b.ReportAllocs() 237 | var r string 238 | for n := 0; n < b.N; n++ { 239 | j := []string{s1, s2} 240 | r = strings.Join(j, "") 241 | } 242 | 243 | result = r 244 | } 245 | 246 | //BenchmarkMapHints bench mymap[string(abytes)] 247 | func BenchmarkMapHints(b *testing.B) { 248 | mymap := make(map[string]string) 249 | mymap["key"] = "value" 250 | abytes := []byte("key") 251 | 252 | b.ResetTimer() 253 | b.ReportAllocs() 254 | 255 | var v string 256 | for n := 0; n < b.N; n++ { 257 | v, _ = mymap[string(abytes)] 258 | } 259 | 260 | result = v 261 | } 262 | 263 | //BenchmarkMapsHints_Dont bench key := string(abytes) 264 | //v, _ = mymap[key] 265 | func BenchmarkMapsHints_Dont(b *testing.B) { 266 | mymap := make(map[string]string) 267 | mymap["key"] = "value" 268 | abytes := []byte("key") 269 | 270 | b.ResetTimer() 271 | b.ReportAllocs() 272 | 273 | var v string 274 | for n := 0; n < b.N; n++ { 275 | key := string(abytes) 276 | v, _ = mymap[key] 277 | } 278 | 279 | result = v 280 | 281 | } 282 | --------------------------------------------------------------------------------