Skip to content

Instantly share code, notes, and snippets.

@TheHippo
Created July 11, 2016 13:42
Show Gist options
  • Save TheHippo/b6dcc4dc7f6aeaa32812704973249018 to your computer and use it in GitHub Desktop.
Save TheHippo/b6dcc4dc7f6aeaa32812704973249018 to your computer and use it in GitHub Desktop.
Append bench
package main
type Wrapper string
type Chain struct {
wrps []Wrapper
}
func (c Chain) Append1(wrps ...Wrapper) Chain {
newWrps := make([]Wrapper, len(c.wrps)+len(wrps))
copy(newWrps, c.wrps)
copy(newWrps[len(c.wrps):], wrps)
return Chain{newWrps}
}
func (c Chain) Append2(wrps ...Wrapper) Chain {
newWrps := make([]Wrapper, 0, len(c.wrps)+len(wrps))
newWrps = append(newWrps, c.wrps...)
newWrps = append(newWrps, wrps...)
return Chain{newWrps}
}
func (c Chain) Append3(wrps ...Wrapper) Chain {
newWrps := make([]Wrapper, 0, len(c.wrps)+len(wrps))
newWrps = append(newWrps, c.wrps...)
return Chain{append(newWrps, wrps...)}
}
func (c Chain) Append4(wrps ...Wrapper) Chain {
newWrps := make([]Wrapper, len(c.wrps), len(c.wrps)+len(wrps))
copy(newWrps, c.wrps)
return Chain{append(newWrps, wrps...)}
}
package main
import (
"testing"
)
func benchmarkAppend1(j int, b *testing.B) {
c := &Chain{
wrps: []Wrapper{"foo", "bar"},
}
for n := 0; n < b.N; n++ {
for i := 0; i < j; i++ {
c.Append1("foobar")
}
}
}
func benchmarkAppend2(j int, b *testing.B) {
c := &Chain{
wrps: []Wrapper{"foo", "bar"},
}
for n := 0; n < b.N; n++ {
for i := 0; i < j; i++ {
c.Append2("foobar")
}
}
}
func benchmarkAppend3(j int, b *testing.B) {
c := &Chain{
wrps: []Wrapper{"foo", "bar"},
}
for n := 0; n < b.N; n++ {
for i := 0; i < j; i++ {
c.Append3("foobar")
}
}
}
func benchmarkAppend4(j int, b *testing.B) {
c := &Chain{
wrps: []Wrapper{"foo", "bar"},
}
for n := 0; n < b.N; n++ {
for i := 0; i < j; i++ {
c.Append4("foobar")
}
}
}
func BenchmarkAppend1_1(b *testing.B) { benchmarkAppend1(1, b) }
func BenchmarkAppend1_10(b *testing.B) { benchmarkAppend1(10, b) }
func BenchmarkAppend1_100(b *testing.B) { benchmarkAppend1(100, b) }
func BenchmarkAppend2_1(b *testing.B) { benchmarkAppend2(1, b) }
func BenchmarkAppend2_10(b *testing.B) { benchmarkAppend2(10, b) }
func BenchmarkAppend2_100(b *testing.B) { benchmarkAppend2(100, b) }
func BenchmarkAppend3_1(b *testing.B) { benchmarkAppend3(1, b) }
func BenchmarkAppend3_10(b *testing.B) { benchmarkAppend3(10, b) }
func BenchmarkAppend3_100(b *testing.B) { benchmarkAppend3(100, b) }
func BenchmarkAppend4_1(b *testing.B) { benchmarkAppend4(1, b) }
func BenchmarkAppend4_10(b *testing.B) { benchmarkAppend4(10, b) }
func BenchmarkAppend4_100(b *testing.B) { benchmarkAppend4(100, b) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment