Skip to content

Instantly share code, notes, and snippets.

@miry
Last active August 29, 2015 14:14
Show Gist options
  • Save miry/a753f33c7881874eda03 to your computer and use it in GitHub Desktop.
Save miry/a753f33c7881874eda03 to your computer and use it in GitHub Desktop.
// RUN: go test -run=XXX -bench=. string_con_test.go
// OUTOUT:
/*
PASS
BenchmarkNaiveConact 2000000000 0.64 ns/op
BenchmarkJoinsConact 3000000 432 ns/op
BenchmarkBuffer 3000000 541 ns/op
ok command-line-arguments 5.262s
*/
package main
import (
"testing"
"strings"
"bytes"
)
func BenchmarkNaiveConact(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = "/games/icons"+"000/000/000"+"/dimension_"+"100_100"+".jpg?"+"123123123123"
}
}
func BenchmarkJoinsConact(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = strings.Join([]string{"/games/icons","000/000/000","/dimension_","100_100",".jpg?","123123123123"}, "")
}
}
func BenchmarkBuffer(b *testing.B) {
for i := 0; i < b.N; i++ {
var buffer bytes.Buffer
buffer.WriteString("/games/icons")
buffer.WriteString("000/000/000")
buffer.WriteString("/dimension_")
buffer.WriteString("100_100")
buffer.WriteString(".jpg?")
buffer.WriteString("123123123123")
_ = buffer.String()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment