2011-05-11 08:35:11 -06:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package runtime_test
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
const N = 20
|
|
|
|
|
2016-04-10 09:32:35 -06:00
|
|
|
func BenchmarkMakeSlice(b *testing.B) {
|
|
|
|
var x []byte
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
x = make([]byte, 32)
|
|
|
|
_ = x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-13 11:58:17 -06:00
|
|
|
func BenchmarkGrowSliceBytes(b *testing.B) {
|
|
|
|
b.StopTimer()
|
2016-03-24 10:42:01 -06:00
|
|
|
var x = make([]byte, 9)
|
2016-03-13 11:58:17 -06:00
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_ = append([]byte(nil), x...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGrowSliceInts(b *testing.B) {
|
|
|
|
b.StopTimer()
|
2016-03-24 10:42:01 -06:00
|
|
|
var x = make([]int, 9)
|
2016-03-13 11:58:17 -06:00
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_ = append([]int(nil), x...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 10:42:01 -06:00
|
|
|
func BenchmarkGrowSlicePtr(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
var x = make([]*byte, 9)
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_ = append([]*byte(nil), x...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type struct24 struct{ a, b, c int64 }
|
|
|
|
|
|
|
|
func BenchmarkGrowSliceStruct24Bytes(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
var x = make([]struct24, 9)
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_ = append([]struct24(nil), x...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-11 08:35:11 -06:00
|
|
|
func BenchmarkAppend(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
x := make([]int, 0, N)
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
x = x[0:0]
|
|
|
|
for j := 0; j < N; j++ {
|
|
|
|
x = append(x, j)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-27 04:11:12 -07:00
|
|
|
func BenchmarkAppendGrowByte(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
var x []byte
|
|
|
|
for j := 0; j < 1<<20; j++ {
|
|
|
|
x = append(x, byte(j))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAppendGrowString(b *testing.B) {
|
|
|
|
var s string
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
var x []string
|
|
|
|
for j := 0; j < 1<<20; j++ {
|
|
|
|
x = append(x, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-01 15:31:26 -07:00
|
|
|
func benchmarkAppendBytes(b *testing.B, length int) {
|
|
|
|
b.StopTimer()
|
|
|
|
x := make([]byte, 0, N)
|
|
|
|
y := make([]byte, length)
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
x = x[0:0]
|
2013-03-02 13:11:05 -07:00
|
|
|
x = append(x, y...)
|
2013-03-01 15:31:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAppend1Byte(b *testing.B) {
|
|
|
|
benchmarkAppendBytes(b, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAppend4Bytes(b *testing.B) {
|
|
|
|
benchmarkAppendBytes(b, 4)
|
|
|
|
}
|
|
|
|
|
2013-08-05 15:51:37 -06:00
|
|
|
func BenchmarkAppend7Bytes(b *testing.B) {
|
|
|
|
benchmarkAppendBytes(b, 7)
|
|
|
|
}
|
|
|
|
|
2013-03-01 15:31:26 -07:00
|
|
|
func BenchmarkAppend8Bytes(b *testing.B) {
|
|
|
|
benchmarkAppendBytes(b, 8)
|
|
|
|
}
|
|
|
|
|
2013-08-05 15:51:37 -06:00
|
|
|
func BenchmarkAppend15Bytes(b *testing.B) {
|
|
|
|
benchmarkAppendBytes(b, 15)
|
|
|
|
}
|
|
|
|
|
2013-03-01 15:31:26 -07:00
|
|
|
func BenchmarkAppend16Bytes(b *testing.B) {
|
|
|
|
benchmarkAppendBytes(b, 16)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAppend32Bytes(b *testing.B) {
|
|
|
|
benchmarkAppendBytes(b, 32)
|
|
|
|
}
|
|
|
|
|
2013-03-01 17:41:39 -07:00
|
|
|
func benchmarkAppendStr(b *testing.B, str string) {
|
|
|
|
b.StopTimer()
|
|
|
|
x := make([]byte, 0, N)
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
x = x[0:0]
|
2013-03-02 13:11:05 -07:00
|
|
|
x = append(x, str...)
|
2013-03-01 17:41:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAppendStr1Byte(b *testing.B) {
|
|
|
|
benchmarkAppendStr(b, "1")
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAppendStr4Bytes(b *testing.B) {
|
|
|
|
benchmarkAppendStr(b, "1234")
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAppendStr8Bytes(b *testing.B) {
|
|
|
|
benchmarkAppendStr(b, "12345678")
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAppendStr16Bytes(b *testing.B) {
|
|
|
|
benchmarkAppendStr(b, "1234567890123456")
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAppendStr32Bytes(b *testing.B) {
|
|
|
|
benchmarkAppendStr(b, "12345678901234567890123456789012")
|
|
|
|
}
|
|
|
|
|
2011-05-11 08:35:11 -06:00
|
|
|
func BenchmarkAppendSpecialCase(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
x := make([]int, 0, N)
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
x = x[0:0]
|
|
|
|
for j := 0; j < N; j++ {
|
|
|
|
if len(x) < cap(x) {
|
|
|
|
x = x[:len(x)+1]
|
|
|
|
x[len(x)-1] = j
|
|
|
|
} else {
|
|
|
|
x = append(x, j)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-14 21:43:03 -06:00
|
|
|
var x []int
|
2011-05-11 08:35:11 -06:00
|
|
|
|
|
|
|
func f() int {
|
|
|
|
x[:1][0] = 3
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSideEffectOrder(t *testing.T) {
|
2011-07-14 21:43:03 -06:00
|
|
|
x = make([]int, 0, 10)
|
2011-05-11 08:35:11 -06:00
|
|
|
x = append(x, 1, f())
|
|
|
|
if x[0] != 1 || x[1] != 2 {
|
|
|
|
t.Error("append failed: ", x[0], x[1])
|
|
|
|
}
|
|
|
|
}
|
2013-03-01 15:31:26 -07:00
|
|
|
|
|
|
|
func TestAppendOverlap(t *testing.T) {
|
|
|
|
x := []byte("1234")
|
|
|
|
x = append(x[1:], x...) // p > q in runtime·appendslice.
|
|
|
|
got := string(x)
|
|
|
|
want := "2341234"
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("overlap failed: got %q want %q", got, want)
|
|
|
|
}
|
|
|
|
}
|
2013-09-11 16:15:28 -06:00
|
|
|
|
|
|
|
func benchmarkCopySlice(b *testing.B, l int) {
|
|
|
|
s := make([]byte, l)
|
|
|
|
buf := make([]byte, 4096)
|
|
|
|
var n int
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
n = copy(buf, s)
|
|
|
|
}
|
|
|
|
b.SetBytes(int64(n))
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkCopyStr(b *testing.B, l int) {
|
|
|
|
s := string(make([]byte, l))
|
|
|
|
buf := make([]byte, 4096)
|
|
|
|
var n int
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
n = copy(buf, s)
|
|
|
|
}
|
|
|
|
b.SetBytes(int64(n))
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkCopy1Byte(b *testing.B) { benchmarkCopySlice(b, 1) }
|
|
|
|
func BenchmarkCopy2Byte(b *testing.B) { benchmarkCopySlice(b, 2) }
|
|
|
|
func BenchmarkCopy4Byte(b *testing.B) { benchmarkCopySlice(b, 4) }
|
|
|
|
func BenchmarkCopy8Byte(b *testing.B) { benchmarkCopySlice(b, 8) }
|
|
|
|
func BenchmarkCopy12Byte(b *testing.B) { benchmarkCopySlice(b, 12) }
|
|
|
|
func BenchmarkCopy16Byte(b *testing.B) { benchmarkCopySlice(b, 16) }
|
|
|
|
func BenchmarkCopy32Byte(b *testing.B) { benchmarkCopySlice(b, 32) }
|
|
|
|
func BenchmarkCopy128Byte(b *testing.B) { benchmarkCopySlice(b, 128) }
|
|
|
|
func BenchmarkCopy1024Byte(b *testing.B) { benchmarkCopySlice(b, 1024) }
|
|
|
|
|
|
|
|
func BenchmarkCopy1String(b *testing.B) { benchmarkCopyStr(b, 1) }
|
|
|
|
func BenchmarkCopy2String(b *testing.B) { benchmarkCopyStr(b, 2) }
|
|
|
|
func BenchmarkCopy4String(b *testing.B) { benchmarkCopyStr(b, 4) }
|
|
|
|
func BenchmarkCopy8String(b *testing.B) { benchmarkCopyStr(b, 8) }
|
|
|
|
func BenchmarkCopy12String(b *testing.B) { benchmarkCopyStr(b, 12) }
|
|
|
|
func BenchmarkCopy16String(b *testing.B) { benchmarkCopyStr(b, 16) }
|
|
|
|
func BenchmarkCopy32String(b *testing.B) { benchmarkCopyStr(b, 32) }
|
|
|
|
func BenchmarkCopy128String(b *testing.B) { benchmarkCopyStr(b, 128) }
|
|
|
|
func BenchmarkCopy1024String(b *testing.B) { benchmarkCopyStr(b, 1024) }
|