Skip to content

Instantly share code, notes, and snippets.

@korylprince
Created September 13, 2015 01:17
Show Gist options
  • Save korylprince/705aa5ecb6564daa0623 to your computer and use it in GitHub Desktop.
Save korylprince/705aa5ecb6564daa0623 to your computer and use it in GitHub Desktop.
package comma
import (
"fmt"
"strings"
)
func Add(one, two string) string {
return one + "," + two
}
func Join(one, two string) string {
return strings.Join([]string{one, two}, ",")
}
func Fmt(one, two string) string {
return fmt.Sprintf("%s,%s", one, two)
}
package comma
import "testing"
//Uses advice from: http://dave.cheney.net/2013/06/30/how-to-write-benchmarks-in-go
var global string
func benchmarkAdd(b *testing.B, one, two string) {
var g string
for n := 0; n < b.N; n++ {
g = Add(one, two)
}
global = g
}
func benchmarkJoin(b *testing.B, one, two string) {
var g string
for n := 0; n < b.N; n++ {
g = Join(one, two)
}
global = g
}
func benchmarkFmt(b *testing.B, one, two string) {
var g string
for n := 0; n < b.N; n++ {
g = Fmt(one, two)
}
global = g
}
func BenchmarkAddShort(b *testing.B) { benchmarkAdd(b, "a", " a") }
func BenchmarkJoinShort(b *testing.B) { benchmarkJoin(b, "a", " a") }
func BenchmarkFmtShort(b *testing.B) { benchmarkFmt(b, "a", " a") }
func BenchmarkAddLong(b *testing.B) {
benchmarkAdd(b, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", " abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz")
}
func BenchmarkJoinLong(b *testing.B) {
benchmarkJoin(b, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", " abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz")
}
func BenchmarkFmtLong(b *testing.B) {
benchmarkFmt(b, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", " abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz")
}
func BenchmarkAddShortLong(b *testing.B) {
benchmarkAdd(b, "a", " abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz")
}
func BenchmarkJoinShortLong(b *testing.B) {
benchmarkJoin(b, "a", " abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz")
}
func BenchmarkFmthortLong(b *testing.B) {
benchmarkFmt(b, "a", " abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment