Last active
April 1, 2016 15:55
-
-
Save mark-rushakoff/b5650bd8f06bece0b9fd to your computer and use it in GitHub Desktop.
Which is faster: `a := make([]int, size); a[i] = val`, or `a := make([]int, 0, size); a = append(a, val)`?
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
→ go test -bench=. -benchmem -benchtime=5s | |
PASS | |
BenchmarkMakeLen_Index8-4 100000000 70.7 ns/op 64 B/op 1 allocs/op | |
BenchmarkMakeLen_Index16-4 100000000 90.7 ns/op 128 B/op 1 allocs/op | |
BenchmarkMakeLen_Index32-4 50000000 132 ns/op 256 B/op 1 allocs/op | |
BenchmarkMakeLen_Index64-4 30000000 212 ns/op 512 B/op 1 allocs/op | |
BenchmarkMakeLen_Index128-4 20000000 362 ns/op 1024 B/op 1 allocs/op | |
BenchmarkMakeLen_Index256-4 10000000 675 ns/op 2048 B/op 1 allocs/op | |
BenchmarkMakeLen_Index512-4 5000000 1371 ns/op 4096 B/op 1 allocs/op | |
BenchmarkMakeLen_Index1024-4 3000000 2573 ns/op 8192 B/op 1 allocs/op | |
BenchmarkMakeLen_Index2048-4 2000000 4674 ns/op 16384 B/op 1 allocs/op | |
BenchmarkMakeCap_Append8-4 100000000 84.3 ns/op 64 B/op 1 allocs/op | |
BenchmarkMakeCap_Append16-4 100000000 111 ns/op 128 B/op 1 allocs/op | |
BenchmarkMakeCap_Append32-4 50000000 166 ns/op 256 B/op 1 allocs/op | |
BenchmarkMakeCap_Append64-4 30000000 283 ns/op 512 B/op 1 allocs/op | |
BenchmarkMakeCap_Append128-4 20000000 533 ns/op 1024 B/op 1 allocs/op | |
BenchmarkMakeCap_Append256-4 10000000 926 ns/op 2048 B/op 1 allocs/op | |
BenchmarkMakeCap_Append512-4 5000000 1819 ns/op 4096 B/op 1 allocs/op | |
BenchmarkMakeCap_Append1024-4 2000000 3575 ns/op 8192 B/op 1 allocs/op | |
BenchmarkMakeCap_Append2048-4 1000000 6604 ns/op 16384 B/op 1 allocs/op | |
BenchmarkSliceAppend8-4 30000000 299 ns/op 120 B/op 4 allocs/op | |
BenchmarkSliceAppend16-4 20000000 382 ns/op 248 B/op 5 allocs/op | |
BenchmarkSliceAppend32-4 20000000 525 ns/op 504 B/op 6 allocs/op | |
BenchmarkSliceAppend64-4 10000000 778 ns/op 1016 B/op 7 allocs/op | |
BenchmarkSliceAppend128-4 10000000 1179 ns/op 2040 B/op 8 allocs/op | |
BenchmarkSliceAppend256-4 5000000 1985 ns/op 4088 B/op 9 allocs/op | |
BenchmarkSliceAppend512-4 2000000 3554 ns/op 8184 B/op 10 allocs/op | |
BenchmarkSliceAppend1024-4 1000000 6362 ns/op 16376 B/op 11 allocs/op | |
BenchmarkSliceAppend2048-4 500000 19058 ns/op 58105 B/op 14 allocs/op | |
ok _/tmp/st 252.621s |
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 slice_test | |
import ( | |
"testing" | |
) | |
var result []int | |
// Just to suppress the "no tests ran" warning | |
func TestNothing(t *testing.T) {} | |
func BenchmarkMakeLen_Index8(b *testing.B) { benchmarkMakeLen_Index(b, 8) } | |
func BenchmarkMakeLen_Index16(b *testing.B) { benchmarkMakeLen_Index(b, 16) } | |
func BenchmarkMakeLen_Index32(b *testing.B) { benchmarkMakeLen_Index(b, 32) } | |
func BenchmarkMakeLen_Index64(b *testing.B) { benchmarkMakeLen_Index(b, 64) } | |
func BenchmarkMakeLen_Index128(b *testing.B) { benchmarkMakeLen_Index(b, 128) } | |
func BenchmarkMakeLen_Index256(b *testing.B) { benchmarkMakeLen_Index(b, 256) } | |
func BenchmarkMakeLen_Index512(b *testing.B) { benchmarkMakeLen_Index(b, 512) } | |
func BenchmarkMakeLen_Index1024(b *testing.B) { benchmarkMakeLen_Index(b, 1024) } | |
func BenchmarkMakeLen_Index2048(b *testing.B) { benchmarkMakeLen_Index(b, 2048) } | |
func BenchmarkMakeCap_Append8(b *testing.B) { benchmarkMakeCap_Append(b, 8) } | |
func BenchmarkMakeCap_Append16(b *testing.B) { benchmarkMakeCap_Append(b, 16) } | |
func BenchmarkMakeCap_Append32(b *testing.B) { benchmarkMakeCap_Append(b, 32) } | |
func BenchmarkMakeCap_Append64(b *testing.B) { benchmarkMakeCap_Append(b, 64) } | |
func BenchmarkMakeCap_Append128(b *testing.B) { benchmarkMakeCap_Append(b, 128) } | |
func BenchmarkMakeCap_Append256(b *testing.B) { benchmarkMakeCap_Append(b, 256) } | |
func BenchmarkMakeCap_Append512(b *testing.B) { benchmarkMakeCap_Append(b, 512) } | |
func BenchmarkMakeCap_Append1024(b *testing.B) { benchmarkMakeCap_Append(b, 1024) } | |
func BenchmarkMakeCap_Append2048(b *testing.B) { benchmarkMakeCap_Append(b, 2048) } | |
func BenchmarkSliceAppend8(b *testing.B) { benchmarkSlice_Append(b, 8) } | |
func BenchmarkSliceAppend16(b *testing.B) { benchmarkSlice_Append(b, 16) } | |
func BenchmarkSliceAppend32(b *testing.B) { benchmarkSlice_Append(b, 32) } | |
func BenchmarkSliceAppend64(b *testing.B) { benchmarkSlice_Append(b, 64) } | |
func BenchmarkSliceAppend128(b *testing.B) { benchmarkSlice_Append(b, 128) } | |
func BenchmarkSliceAppend256(b *testing.B) { benchmarkSlice_Append(b, 256) } | |
func BenchmarkSliceAppend512(b *testing.B) { benchmarkSlice_Append(b, 512) } | |
func BenchmarkSliceAppend1024(b *testing.B) { benchmarkSlice_Append(b, 1024) } | |
func BenchmarkSliceAppend2048(b *testing.B) { benchmarkSlice_Append(b, 2048) } | |
func benchmarkMakeLen_Index(b *testing.B, sliceLen int) { | |
for i := 0; i < b.N; i++ { | |
// Initialize slice to exact length | |
a := make([]int, sliceLen) | |
for j := 0; j < sliceLen; j++ { | |
a[j] = j | |
} | |
result = a | |
} | |
} | |
func benchmarkMakeCap_Append(b *testing.B, sliceCap int) { | |
for i := 0; i < b.N; i++ { | |
// Initialize slice with 0 length and known capacity | |
a := make([]int, 0, sliceCap) | |
for j := 0; j < sliceCap; j++ { | |
a = append(a, j) | |
} | |
result = a | |
} | |
} | |
func benchmarkSlice_Append(b *testing.B, length int) { | |
for i := 0; i < b.N; i++ { | |
// Use an empty slice literal and repeatedly append | |
a := []int{} | |
for j := 0; j < length; j++ { | |
a = append(a, j) | |
} | |
result = a | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment