Created
February 25, 2015 15:14
-
-
Save beatrichartz/27431e641e9f4a74ab60 to your computer and use it in GitHub Desktop.
Difference for capacity
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 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() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
run with
go test -bench=.