Skip to content

Instantly share code, notes, and snippets.

@capoferro
Created September 17, 2014 22:48
Show Gist options
  • Save capoferro/2e957b25080ad1e7726f to your computer and use it in GitHub Desktop.
Save capoferro/2e957b25080ad1e7726f to your computer and use it in GitHub Desktop.
package main
import (
"testing"
)
func BenchmarkAaa(b *testing.B) {
ints := make([]int, 0)
for i := 0; i < b.N; i++ {
ints = append(ints, i)
}
}
func BenchmarkBbb(b *testing.B) {
ints := make([]int, b.N)
for i := 0; i < b.N; i++ {
ints[i] = i
}
}
@capoferro
Copy link
Author

BenchmarkAaa 200000000 8.36 ns/op
BenchmarkBbb 2000000000 39.1 ns/op

@capoferro
Copy link
Author

BenchmarkBbb 2000000000 31.5 ns/op
BenchmarkAaa 50000000 27.8 ns/op

@capoferro
Copy link
Author

BenchmarkBbb 2000000000 36.0 ns/op
BenchmarkAaa 100000000 23.1 ns/op

@capoferro
Copy link
Author

BenchmarkAaa 200000000 7.62 ns/op
BenchmarkBbb 2000000000 36.7 ns/op

@capoferro
Copy link
Author

For some reason flipping the run order of the benchmarks makes Aaa take much longer.

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