Commit 4f43201e authored by Ewan Chou's avatar Ewan Chou Committed by Russ Cox

strings: remove allocations in Split(s, "")

BenchmarkSplit1     77984460     24131380  -69.06%

R=golang-dev, rsc, minux.ma, dave, extemporalgenome
CC=golang-dev
https://golang.org/cl/7458043
parent 64eec932
......@@ -26,7 +26,11 @@ func explode(s string, n int) []string {
i, cur := 0, 0
for ; i+1 < n; i++ {
ch, size = utf8.DecodeRuneInString(s[cur:])
a[i] = string(ch)
if ch == utf8.RuneError {
a[i] = string(utf8.RuneError)
} else {
a[i] = s[cur : cur+size]
}
cur += size
}
// add the rest, if there is any
......
......@@ -1095,3 +1095,21 @@ func BenchmarkFieldsFunc(b *testing.B) {
FieldsFunc(fieldsInput, unicode.IsSpace)
}
}
func BenchmarkSplit1(b *testing.B) {
for i := 0; i < b.N; i++ {
Split(benchInputHard, "")
}
}
func BenchmarkSplit2(b *testing.B) {
for i := 0; i < b.N; i++ {
Split(benchInputHard, "/")
}
}
func BenchmarkSplit3(b *testing.B) {
for i := 0; i < b.N; i++ {
Split(benchInputHard, "hello")
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment