Created
October 7, 2015 18:09
-
-
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
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 ( | |
"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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results from my old single core laptop: