Skip to content

Instantly share code, notes, and snippets.

@jucrouzet
Last active July 12, 2018 17:13
Show Gist options
  • Save jucrouzet/0506e6bc80fd1d344f4ed6639f5f5a26 to your computer and use it in GitHub Desktop.
Save jucrouzet/0506e6bc80fd1d344f4ed6639f5f5a26 to your computer and use it in GitHub Desktop.
package main
import "fmt"
func main() {
fmt.Printf("%v\n", WithoutCap())
fmt.Printf("%v\n", WithCap())
}
func WithoutCap() []int {
slice := make([]int, 0)
for i := 0; i <= 100000; i++ {
slice = append(slice, i)
}
return slice
}
func WithCap() []int {
slice := make([]int, 0, 100000)
for i := 0; i <= 100000; i++ {
slice = append(slice, i)
}
return slice
}
package main
import "testing"
func BenchmarkWithoutCap(b *testing.B) {
for n := 0; n < b.N; n++ {
WithoutCap()
}
}
func BenchmarkWithCap(b *testing.B) {
for n := 0; n < b.N; n++ {
WithCap()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment