Created
September 19, 2017 17:03
-
-
Save gbbr/f20a223c144d0f8cc80bce2473e7f306 to your computer and use it in GitHub Desktop.
Append Speeds Benchmark
This file contains 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" | |
func Append(str ...string) string { | |
var b []byte | |
for _, s := range str { | |
b = append(b, s...) | |
} | |
return string(b) | |
} | |
func AppendBuf(str ...string) string { | |
b := bytes.NewBufferString(str[0]) | |
for i := 1; i < len(str); i++ { | |
b.WriteString(str[i]) | |
} | |
return b.String() | |
} |
This file contains 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 "testing" | |
var result string | |
func benchAppend(b *testing.B, str ...string) { | |
var s string | |
for n := 0; n < b.N; n++ { | |
s = Append(str...) | |
} | |
result = s | |
} | |
func benchAppendBuf(b *testing.B, str ...string) { | |
var s string | |
for n := 0; n < b.N; n++ { | |
s = AppendBuf(str...) | |
} | |
result = s | |
} | |
func BenchmarkAppend3(b *testing.B) { | |
benchAppend(b, []string{"fakdsj", "fakdsj", "fakdsj"}...) | |
} | |
func BenchmarkAppend9(b *testing.B) { | |
benchAppend(b, []string{ | |
"fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", | |
"fakdsj", "fakdsj", "fakdsj", | |
}...) | |
} | |
func BenchmarkAppend36(b *testing.B) { | |
benchAppend(b, []string{ | |
"fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", | |
"fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", | |
"fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", | |
"fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", | |
"fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", | |
"fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", | |
}...) | |
} | |
func BenchmarkBuf3(b *testing.B) { | |
benchAppendBuf(b, []string{"fakdsj", "fakdsj", "fakdsj"}...) | |
} | |
func BenchmarkBuf9(b *testing.B) { | |
benchAppendBuf(b, []string{ | |
"fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", | |
"fakdsj", "fakdsj", "fakdsj", | |
}...) | |
} | |
func BenchmarkBuf36(b *testing.B) { | |
benchAppendBuf(b, []string{ | |
"fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", | |
"fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", | |
"fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", | |
"fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", | |
"fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", | |
"fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", "fakdsj", | |
}...) | |
} |
This file contains 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
gbbr@mecca:~ (master)$ go test -bench=. -benchmem | |
goos: darwin | |
goarch: amd64 | |
BenchmarkAppend3-8 10000000 158 ns/op 88 B/op 4 allocs/op | |
BenchmarkAppend9-8 5000000 240 ns/op 184 B/op 5 allocs/op | |
BenchmarkAppend36-8 3000000 492 ns/op 728 B/op 7 allocs/op | |
BenchmarkBuf3-8 5000000 278 ns/op 184 B/op 4 allocs/op | |
BenchmarkBuf9-8 2000000 581 ns/op 392 B/op 6 allocs/op | |
BenchmarkBuf36-8 1000000 1065 ns/op 776 B/op 7 allocs/op | |
PASS | |
ok _/Users/Gabriel 9.728s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment