Created
September 15, 2016 21:16
-
-
Save GeorgeErickson/2a2433892e0a0e4cb6b0542088db0973 to your computer and use it in GitHub Desktop.
string bench
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"fmt" | |
"math" | |
"strconv" | |
"strings" | |
"sync" | |
"testing" | |
) | |
var bufPool = sync.Pool{ | |
New: func() interface{} { return new(bytes.Buffer) }, | |
} | |
type benchFunc func(i int64) []byte | |
type benchFuncString func(i int64) string | |
func BenchmarkStringBuild(t *testing.B) { | |
exp := "9223372036854775807-9223372036854775807-9223372036854775807" | |
casesByte := map[string]benchFunc{ | |
"strconv.AppendInt": func(i int64) []byte { | |
b := make([]byte, 0, 8*3+2) | |
b = strconv.AppendInt(b, i, 10) | |
b = append(b, '-') | |
b = strconv.AppendInt(b, i, 10) | |
b = append(b, '-') | |
b = strconv.AppendInt(b, i, 10) | |
return b | |
}, | |
"bytes.Buffer": func(i int64) []byte { | |
var b bytes.Buffer | |
b.WriteString(strconv.FormatInt(i, 10)) | |
b.WriteByte('-') | |
b.WriteString(strconv.FormatInt(i, 10)) | |
b.WriteByte('-') | |
b.WriteString(strconv.FormatInt(i, 10)) | |
return b.Bytes() | |
}, | |
"bytes.Buffer w pool": func(i int64) []byte { | |
b := bufPool.Get().(*bytes.Buffer) | |
b.Reset() | |
b.WriteString(strconv.FormatInt(i, 10)) | |
b.WriteByte('-') | |
b.WriteString(strconv.FormatInt(i, 10)) | |
b.WriteByte('-') | |
b.WriteString(strconv.FormatInt(i, 10)) | |
out := make([]byte, b.Len()) | |
copy(out, b.Bytes()) | |
bufPool.Put(b) | |
return out | |
}, | |
} | |
casesString := map[string]benchFuncString{ | |
"fmt": func(i int64) string { | |
return fmt.Sprintf("%d-%d-%d", i, i, i) | |
}, | |
"+ + +": func(i int64) string { | |
return strconv.FormatInt(i, 10) + "-" + strconv.FormatInt(i, 10) + "-" + strconv.FormatInt(i, 10) | |
}, | |
"strings.Join": func(i int64) string { | |
return strings.Join([]string{strconv.FormatInt(i, 10), strconv.FormatInt(i, 10), strconv.FormatInt(i, 10)}, "-") | |
}, | |
} | |
for name, fn := range casesByte { | |
out := fn(math.MaxInt64) | |
if string(out) != exp { | |
t.Fatal(name, string(out)) | |
} | |
t.Run(name, func(tb *testing.B) { | |
for i := int64(0); i < int64(tb.N); i++ { | |
out = fn(i) | |
} | |
}) | |
} | |
for name, fn := range casesString { | |
out := fn(math.MaxInt64) | |
if out != exp { | |
t.Fatal(name, out) | |
} | |
t.Run(name, func(tb *testing.B) { | |
for i := int64(0); i < int64(tb.N); i++ { | |
out = fn(i) | |
} | |
}) | |
} | |
} |
Author
GeorgeErickson
commented
Sep 15, 2016
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment