Skip to content

Instantly share code, notes, and snippets.

@klobucar
Last active December 16, 2015 07:19
Show Gist options
  • Save klobucar/5398044 to your computer and use it in GitHub Desktop.
Save klobucar/5398044 to your computer and use it in GitHub Desktop.
Ran some becnhmarks on the following code.
package concat_test
import (
"fmt"
"strconv"
"strings"
"testing"
)
var ipAddr string = "127.0.0.1"
var PORT string = "8080"
var port int = 8080
func BenchmarkConcatStrconv(b *testing.B) {
for i := 0; i < b.N; i++ {
fmt.Sprintf("http://" + ipAddr + ":" + strconv.Itoa(port) + "/v1/api/1")
}
}
func BenchmarkConcat(b *testing.B) {
for i := 0; i < b.N; i++ {
// assuming port is a string here
fmt.Sprintf("http://" + ipAddr + ":" + PORT + "/v1/api/1")
}
}
func BenchmarkSprintFDigit(b *testing.B) {
for i := 0; i < b.N; i++ {
fmt.Sprintf(fmt.Sprintf("http://%s:%d/api/1", ipAddr, port))
}
}
func BenchmarkSprintF(b *testing.B) {
for i := 0; i < b.N; i++ {
fmt.Sprintf(fmt.Sprintf("http://%s:%s/api/1", ipAddr, PORT))
}
}
func BenchmarkStringsJoin(b *testing.B) {
for i := 0; i < b.N; i++ {
fmt.Sprintf(strings.Join([]string{"http://", ipAddr, ":", PORT, "/v1/api/1"}, ""))
}
}
func BenchmarkStringsJoinStrconv(b *testing.B) {
for i := 0; i < b.N; i++ {
fmt.Sprintf(strings.Join([]string{"http://", ipAddr, ":", strconv.Itoa(port), "/v1/api/1"}, ""))
}
}

OS X 10.8

#Results

Go 1.0.3

go test -v . -bench=".*"
testing: warning: no tests to run
PASS
enchmarkConcatStrconv   5000000	       611 ns/op
BenchmarkConcat	 5000000	       474 ns/op
BenchmarkSprintFDigit	 1000000	      1010 ns/op
BenchmarkSprintF	 1000000	      1057 ns/op
BenchmarkStringsJoin	 5000000	       654 ns/op
BenchmarkStringsJoinStrconv	 2000000	       789 ns/op

Go 1.1beta2

go test -v . -bench=".*"
testing: warning: no tests to run
PASS
BenchmarkConcatStrconv   5000000	       491 ns/op
BenchmarkConcat	 5000000	       395 ns/op
BenchmarkSprintFDigit	 5000000	       667 ns/op
BenchmarkSprintF	 5000000	       668 ns/op
BenchmarkStringsJoin	 5000000	       524 ns/op
BenchmarkStringsJoinStrconv	 5000000	       616 ns/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment