Skip to content

Instantly share code, notes, and snippets.

@tarrsalah
Created April 16, 2015 18:23
Show Gist options
  • Select an option

  • Save tarrsalah/74e663fa3eb2e074a344 to your computer and use it in GitHub Desktop.

Select an option

Save tarrsalah/74e663fa3eb2e074a344 to your computer and use it in GitHub Desktop.
append
package slices
import (
"testing"
)
var length = 10000
func BenchmarkAppend(b *testing.B) {
for i := 0; i < b.N; i++ {
Append()
}
}
func BenchmarkAppendFixed(b *testing.B) {
for i := 0; i < b.N; i++ {
AppendFixed()
}
}
func Append() {
var b []int
for i := 0; i < length; i++ {
b = append(b, i)
}
}
func AppendFixed() {
b := make([]int, 0, length)
for i := 0; i < length; i++ {
b = append(b, i)
}
}
PASS
BenchmarkAppend 3000 430755 ns/op
BenchmarkAppendFixed 10000 121799 ns/op
ok github.com/tarrsalah/misc/spec/slices 2.572s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment