Created
October 3, 2019 09:01
-
-
Save hnakamur/a3cc6567fe69cf0b7669f862a2043d96 to your computer and use it in GitHub Desktop.
Go benchmark StringBuilder with Fprintf
This file contains hidden or 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 ( | |
"fmt" | |
"strconv" | |
"strings" | |
"testing" | |
) | |
func dummy(s string) {} | |
func BenchmarkStringBuilder(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var b strings.Builder | |
b.WriteString("a=") | |
fmt.Fprintf(&b, "%d", 12345) | |
dummy(b.String()) | |
} | |
} | |
func BenchmarkByteSlice(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var b []byte | |
b = append(b, "a="...) | |
b = strconv.AppendInt(b, 12345, 10) | |
dummy(string(b)) | |
} | |
} | |
func BenchmarkStringList(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var b []string | |
b = append(b, "a=") | |
b = append(b, fmt.Sprintf("%d", 12345)) | |
dummy(strings.Join(b, "")) | |
} | |
} |
Author
hnakamur
commented
Oct 3, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment