Skip to content

Instantly share code, notes, and snippets.

@benjbaron
Created March 28, 2019 09:08
Show Gist options
  • Save benjbaron/83b4a1325f43413dd0ccc0f854c52e1b to your computer and use it in GitHub Desktop.
Save benjbaron/83b4a1325f43413dd0ccc0f854c52e1b to your computer and use it in GitHub Desktop.
func Benchmark_append(b *testing.B) {
b.StopTimer()
b.ReportAllocs()
b.StartTimer()
for i := 0; i < 1000; i++ {
var a []int
for i := 0; i < 100000; i++ {
a = append(a, i)
}
}
}
func Benchmark_noappend(b *testing.B) {
b.StopTimer()
b.ReportAllocs()
b.StartTimer()
for i := 0; i < 1000; i++ {
var a = make([]int, 100000)
for i := 0; i < 100000; i++ {
a[i] = i
}
}
}
@benjbaron
Copy link
Author

Results:

Benchmark_noappend-8          10000        113692 ns/op      802816 B/op           1 allocs/op
Benchmark_append-8           3000        469265 ns/op     4654338 B/op          30 allocs/op

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment