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 BenchmarkAppendGrowByte(b
*testing
.B
) {
23 for i
:= 0; i
< b
.N
; i
++ {
25 for j
:= 0; j
< 1<<20; j
++ {
26 x
= append(x
, byte(j
))
31 func BenchmarkAppendGrowString(b
*testing
.B
) {
33 for i
:= 0; i
< b
.N
; i
++ {
35 for j
:= 0; j
< 1<<20; j
++ {
41 func benchmarkAppendBytes(b
*testing
.B
, length
int) {
43 x
:= make([]byte, 0, N
)
44 y
:= make([]byte, length
)
46 for i
:= 0; i
< b
.N
; i
++ {
52 func BenchmarkAppend1Byte(b
*testing
.B
) {
53 benchmarkAppendBytes(b
, 1)
56 func BenchmarkAppend4Bytes(b
*testing
.B
) {
57 benchmarkAppendBytes(b
, 4)
60 func BenchmarkAppend7Bytes(b
*testing
.B
) {
61 benchmarkAppendBytes(b
, 7)
64 func BenchmarkAppend8Bytes(b
*testing
.B
) {
65 benchmarkAppendBytes(b
, 8)
68 func BenchmarkAppend15Bytes(b
*testing
.B
) {
69 benchmarkAppendBytes(b
, 15)
72 func BenchmarkAppend16Bytes(b
*testing
.B
) {
73 benchmarkAppendBytes(b
, 16)
76 func BenchmarkAppend32Bytes(b
*testing
.B
) {
77 benchmarkAppendBytes(b
, 32)
80 func benchmarkAppendStr(b
*testing
.B
, str
string) {
82 x
:= make([]byte, 0, N
)
84 for i
:= 0; i
< b
.N
; i
++ {
90 func BenchmarkAppendStr1Byte(b
*testing
.B
) {
91 benchmarkAppendStr(b
, "1")
94 func BenchmarkAppendStr4Bytes(b
*testing
.B
) {
95 benchmarkAppendStr(b
, "1234")
98 func BenchmarkAppendStr8Bytes(b
*testing
.B
) {
99 benchmarkAppendStr(b
, "12345678")
102 func BenchmarkAppendStr16Bytes(b
*testing
.B
) {
103 benchmarkAppendStr(b
, "1234567890123456")
106 func BenchmarkAppendStr32Bytes(b
*testing
.B
) {
107 benchmarkAppendStr(b
, "12345678901234567890123456789012")
110 func BenchmarkAppendSpecialCase(b
*testing
.B
) {
112 x
:= make([]int, 0, N
)
114 for i
:= 0; i
< b
.N
; i
++ {
116 for j
:= 0; j
< N
; j
++ {
134 func TestSideEffectOrder(t
*testing
.T
) {
135 x
= make([]int, 0, 10)
136 x
= append(x
, 1, f())
137 if x
[0] != 1 || x
[1] != 2 {
138 t
.Error("append failed: ", x
[0], x
[1])
142 func TestAppendOverlap(t
*testing
.T
) {
144 x
= append(x
[1:], x
...) // p > q in runtimeĀ·appendslice.
148 t
.Errorf("overlap failed: got %q want %q", got
, want
)
152 func benchmarkCopySlice(b
*testing
.B
, l
int) {
154 buf
:= make([]byte, 4096)
156 for i
:= 0; i
< b
.N
; i
++ {
162 func benchmarkCopyStr(b
*testing
.B
, l
int) {
163 s
:= string(make([]byte, l
))
164 buf
:= make([]byte, 4096)
166 for i
:= 0; i
< b
.N
; i
++ {
172 func BenchmarkCopy1Byte(b
*testing
.B
) { benchmarkCopySlice(b
, 1) }
173 func BenchmarkCopy2Byte(b
*testing
.B
) { benchmarkCopySlice(b
, 2) }
174 func BenchmarkCopy4Byte(b
*testing
.B
) { benchmarkCopySlice(b
, 4) }
175 func BenchmarkCopy8Byte(b
*testing
.B
) { benchmarkCopySlice(b
, 8) }
176 func BenchmarkCopy12Byte(b
*testing
.B
) { benchmarkCopySlice(b
, 12) }
177 func BenchmarkCopy16Byte(b
*testing
.B
) { benchmarkCopySlice(b
, 16) }
178 func BenchmarkCopy32Byte(b
*testing
.B
) { benchmarkCopySlice(b
, 32) }
179 func BenchmarkCopy128Byte(b
*testing
.B
) { benchmarkCopySlice(b
, 128) }
180 func BenchmarkCopy1024Byte(b
*testing
.B
) { benchmarkCopySlice(b
, 1024) }
182 func BenchmarkCopy1String(b
*testing
.B
) { benchmarkCopyStr(b
, 1) }
183 func BenchmarkCopy2String(b
*testing
.B
) { benchmarkCopyStr(b
, 2) }
184 func BenchmarkCopy4String(b
*testing
.B
) { benchmarkCopyStr(b
, 4) }
185 func BenchmarkCopy8String(b
*testing
.B
) { benchmarkCopyStr(b
, 8) }
186 func BenchmarkCopy12String(b
*testing
.B
) { benchmarkCopyStr(b
, 12) }
187 func BenchmarkCopy16String(b
*testing
.B
) { benchmarkCopyStr(b
, 16) }
188 func BenchmarkCopy32String(b
*testing
.B
) { benchmarkCopyStr(b
, 32) }
189 func BenchmarkCopy128String(b
*testing
.B
) { benchmarkCopyStr(b
, 128) }
190 func BenchmarkCopy1024String(b
*testing
.B
) { benchmarkCopyStr(b
, 1024) }