1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
10 func BenchmarkAppend(b
*testing
.B
) {
12 x
:= make([]int, 0, N
)
14 for i
:= 0; i
< b
.N
; i
++ {
16 for j
:= 0; j
< N
; j
++ {
22 func benchmarkAppendBytes(b
*testing
.B
, length
int) {
24 x
:= make([]byte, 0, N
)
25 y
:= make([]byte, length
)
27 for i
:= 0; i
< b
.N
; i
++ {
33 func BenchmarkAppend1Byte(b
*testing
.B
) {
34 benchmarkAppendBytes(b
, 1)
37 func BenchmarkAppend4Bytes(b
*testing
.B
) {
38 benchmarkAppendBytes(b
, 4)
41 func BenchmarkAppend7Bytes(b
*testing
.B
) {
42 benchmarkAppendBytes(b
, 7)
45 func BenchmarkAppend8Bytes(b
*testing
.B
) {
46 benchmarkAppendBytes(b
, 8)
49 func BenchmarkAppend15Bytes(b
*testing
.B
) {
50 benchmarkAppendBytes(b
, 15)
53 func BenchmarkAppend16Bytes(b
*testing
.B
) {
54 benchmarkAppendBytes(b
, 16)
57 func BenchmarkAppend32Bytes(b
*testing
.B
) {
58 benchmarkAppendBytes(b
, 32)
61 func benchmarkAppendStr(b
*testing
.B
, str
string) {
63 x
:= make([]byte, 0, N
)
65 for i
:= 0; i
< b
.N
; i
++ {
71 func BenchmarkAppendStr1Byte(b
*testing
.B
) {
72 benchmarkAppendStr(b
, "1")
75 func BenchmarkAppendStr4Bytes(b
*testing
.B
) {
76 benchmarkAppendStr(b
, "1234")
79 func BenchmarkAppendStr8Bytes(b
*testing
.B
) {
80 benchmarkAppendStr(b
, "12345678")
83 func BenchmarkAppendStr16Bytes(b
*testing
.B
) {
84 benchmarkAppendStr(b
, "1234567890123456")
87 func BenchmarkAppendStr32Bytes(b
*testing
.B
) {
88 benchmarkAppendStr(b
, "12345678901234567890123456789012")
91 func BenchmarkAppendSpecialCase(b
*testing
.B
) {
93 x
:= make([]int, 0, N
)
95 for i
:= 0; i
< b
.N
; i
++ {
97 for j
:= 0; j
< N
; j
++ {
115 func TestSideEffectOrder(t
*testing
.T
) {
116 x
= make([]int, 0, 10)
117 x
= append(x
, 1, f())
118 if x
[0] != 1 || x
[1] != 2 {
119 t
.Error("append failed: ", x
[0], x
[1])
123 func TestAppendOverlap(t
*testing
.T
) {
125 x
= append(x
[1:], x
...) // p > q in runtimeĀ·appendslice.
129 t
.Errorf("overlap failed: got %q want %q", got
, want
)
133 func benchmarkCopySlice(b
*testing
.B
, l
int) {
135 buf
:= make([]byte, 4096)
137 for i
:= 0; i
< b
.N
; i
++ {
143 func benchmarkCopyStr(b
*testing
.B
, l
int) {
144 s
:= string(make([]byte, l
))
145 buf
:= make([]byte, 4096)
147 for i
:= 0; i
< b
.N
; i
++ {
153 func BenchmarkCopy1Byte(b
*testing
.B
) { benchmarkCopySlice(b
, 1) }
154 func BenchmarkCopy2Byte(b
*testing
.B
) { benchmarkCopySlice(b
, 2) }
155 func BenchmarkCopy4Byte(b
*testing
.B
) { benchmarkCopySlice(b
, 4) }
156 func BenchmarkCopy8Byte(b
*testing
.B
) { benchmarkCopySlice(b
, 8) }
157 func BenchmarkCopy12Byte(b
*testing
.B
) { benchmarkCopySlice(b
, 12) }
158 func BenchmarkCopy16Byte(b
*testing
.B
) { benchmarkCopySlice(b
, 16) }
159 func BenchmarkCopy32Byte(b
*testing
.B
) { benchmarkCopySlice(b
, 32) }
160 func BenchmarkCopy128Byte(b
*testing
.B
) { benchmarkCopySlice(b
, 128) }
161 func BenchmarkCopy1024Byte(b
*testing
.B
) { benchmarkCopySlice(b
, 1024) }
163 func BenchmarkCopy1String(b
*testing
.B
) { benchmarkCopyStr(b
, 1) }
164 func BenchmarkCopy2String(b
*testing
.B
) { benchmarkCopyStr(b
, 2) }
165 func BenchmarkCopy4String(b
*testing
.B
) { benchmarkCopyStr(b
, 4) }
166 func BenchmarkCopy8String(b
*testing
.B
) { benchmarkCopyStr(b
, 8) }
167 func BenchmarkCopy12String(b
*testing
.B
) { benchmarkCopyStr(b
, 12) }
168 func BenchmarkCopy16String(b
*testing
.B
) { benchmarkCopyStr(b
, 16) }
169 func BenchmarkCopy32String(b
*testing
.B
) { benchmarkCopyStr(b
, 32) }
170 func BenchmarkCopy128String(b
*testing
.B
) { benchmarkCopyStr(b
, 128) }
171 func BenchmarkCopy1024String(b
*testing
.B
) { benchmarkCopyStr(b
, 1024) }