Last active
July 12, 2018 17:13
-
-
Save jucrouzet/0506e6bc80fd1d344f4ed6639f5f5a26 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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