Skip to content

Instantly share code, notes, and snippets.

@montanaflynn
Created October 7, 2015 18:09
Show Gist options
  • Save montanaflynn/180b897bb2945ec36b9e to your computer and use it in GitHub Desktop.
Save montanaflynn/180b897bb2945ec36b9e to your computer and use it in GitHub Desktop.
Benchmarks for different ways of concatenating strings and turning an int into a string
package main
import (
"bytes"
"fmt"
"strconv"
"testing"
)
var (
host string
port string
)
func init() {
host = "localhost"
port = ":4444"
}
func checkConcatatedString(t *testing.T, concatStr string) {
expected := "localhost:4444"
if concatStr != expected {
t.Fatal(concatStr, "!=", expected)
}
}
func checkIntString(t *testing.T, intStr string) {
expected := "4444"
if intStr != expected {
t.Fatal(intStr, "!=", expected)
}
}
// String concatenation using plus sign
func ConcatStringBuiltin(x string, y string) string {
return string(x + y)
}
func TestConcatStringBuiltin(t *testing.T) {
str := ConcatStringBuiltin(host, port)
checkConcatatedString(t, str)
}
func BenchmarkConcatStringBuiltin(b *testing.B) {
for n := 0; n < b.N; n++ {
ConcatStringBuiltin(host, port)
}
}
// String concatenation using bytes.Buffer
func ConcatStringBuffer(x string, y string) string {
var buffer bytes.Buffer
buffer.WriteString(x)
buffer.WriteString(y)
return buffer.String()
}
func TestConcatStringBuffer(t *testing.T) {
str := ConcatStringBuffer(host, port)
checkConcatatedString(t, str)
}
func BenchmarkConcatStringBuffer(b *testing.B) {
for n := 0; n < b.N; n++ {
ConcatStringBuffer(host, port)
}
}
// String concatenation using fmt.Sprintf
func ConcatStringSprintf(x string, y string) string {
return fmt.Sprintf("%s%s", x, y)
}
func TestConcatStringSprintf(t *testing.T) {
str := ConcatStringSprintf(host, port)
checkConcatatedString(t, str)
}
func BenchmarkConcatStringSprintf(b *testing.B) {
for n := 0; n < b.N; n++ {
ConcatStringSprintf(host, port)
}
}
// IntToString using strconv Itoa
func IntToStringItoa(i int) string {
return strconv.Itoa(i)
}
func TestIntToStringItoa(t *testing.T) {
checkIntString(t, IntToStringItoa(4444))
}
func BenchmarkIntToStringItoa(b *testing.B) {
for n := 0; n < b.N; n++ {
IntToStringItoa(4444)
}
}
// IntToString using strconv ParseInt
func IntToStringParseInt(i int) string {
return strconv.Itoa(i)
}
func TestIntToStringParseInt(t *testing.T) {
checkIntString(t, IntToStringParseInt(4444))
}
func BenchmarkIntToStringParseInt(b *testing.B) {
for n := 0; n < b.N; n++ {
IntToStringParseInt(4444)
}
}
// IntToString using fmt.Sprintf
func IntToStringSprintf(i int) string {
return fmt.Sprintf("%d", i)
}
func TestIntToStringSprintf(t *testing.T) {
i := IntToStringSprintf(4444)
checkIntString(t, i)
}
func BenchmarkIntToStringSprintf(b *testing.B) {
for n := 0; n < b.N; n++ {
IntToStringSprintf(4444)
}
}
@montanaflynn
Copy link
Author

Results from my old single core laptop:

$ go test -v -benchmem -bench=.
PASS
BenchmarkConcatStringBuiltin      3000000      488 ns/op     16 B/op     1 allocs/op
BenchmarkConcatStringBuffer       1000000     1796 ns/op    128 B/op     2 allocs/op
BenchmarkConcatStringSprintf       500000     2699 ns/op     48 B/op     3 allocs/op
BenchmarkIntToStringItoa          5000000      334 ns/op      4 B/op     1 allocs/op
BenchmarkIntToStringParseInt      5000000      328 ns/op      4 B/op     1 allocs/op
BenchmarkIntToStringSprintf       1000000     2041 ns/op     16 B/op     2 allocs/op

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment