Skip to content

Instantly share code, notes, and snippets.

@beatrichartz
Created February 25, 2015 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beatrichartz/27431e641e9f4a74ab60 to your computer and use it in GitHub Desktop.
Save beatrichartz/27431e641e9f4a74ab60 to your computer and use it in GitHub Desktop.
Difference for capacity
package captest
import "testing"
func WithCap() {
sl := make([]int, 500, 1000)
for i := 0; i < 1000; i++ {
if i < 500 {
sl[i] = i
} else {
sl = append(sl, i)
}
}
}
func WithoutCap() {
sl := make([]int, 500)
for i := 0; i < 1000; i++ {
if i < 500 {
sl[i] = i
} else {
sl = append(sl, i)
}
}
}
func BenchmarkWithCap(b *testing.B) {
// run the Fib function b.N times
for n := 0; n < b.N; n++ {
WithCap()
}
}
func BenchmarkWithoutCap(b *testing.B) {
// run the Fib function b.N times
for n := 0; n < b.N; n++ {
WithoutCap()
}
}
@beatrichartz
Copy link
Author

run with go test -bench=.

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