summaryrefslogtreecommitdiff
path: root/libgo/go/container/vector/numbers_test.go
blob: d540ace0502140450c98bcb4c0a44182cf3ff5cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2009 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 vector

import (
	"fmt"
	"runtime"
	"strings"
	"testing"
)


const memTestN = 1000000


func s(n uint64) string {
	str := fmt.Sprintf("%d", n)
	lens := len(str)
	a := make([]string, (lens+2)/3)
	start := lens
	for i := range a {
		start -= 3
		if start < 0 {
			start = 0
		}
		a[len(a)-i-1] = str[start:lens]
		lens -= 3
	}
	return strings.Join(a, " ")
}


func TestVectorNums(t *testing.T) {
	var v Vector
	c := int(0)
	runtime.GC()
	m0 := runtime.MemStats
	v.Resize(memTestN, memTestN)
	for i := 0; i < memTestN; i++ {
		v.Set(i, c)
	}
	runtime.GC()
	m := runtime.MemStats
	v.Resize(0, 0)
	runtime.GC()
	n := m.Alloc - m0.Alloc
	t.Logf("%T.Push(%#v), n = %s: Alloc/n = %.2f\n", v, c, s(memTestN), float64(n)/memTestN)
}


func TestIntVectorNums(t *testing.T) {
	var v IntVector
	c := int(0)
	runtime.GC()
	m0 := runtime.MemStats
	v.Resize(memTestN, memTestN)
	for i := 0; i < memTestN; i++ {
		v.Set(i, c)
	}
	runtime.GC()
	m := runtime.MemStats
	v.Resize(0, 0)
	runtime.GC()
	n := m.Alloc - m0.Alloc
	t.Logf("%T.Push(%#v), n = %s: Alloc/n = %.2f\n", v, c, s(memTestN), float64(n)/memTestN)
}


func TestStringVectorNums(t *testing.T) {
	var v StringVector
	c := ""
	runtime.GC()
	m0 := runtime.MemStats
	v.Resize(memTestN, memTestN)
	for i := 0; i < memTestN; i++ {
		v.Set(i, c)
	}
	runtime.GC()
	m := runtime.MemStats
	v.Resize(0, 0)
	runtime.GC()
	n := m.Alloc - m0.Alloc
	t.Logf("%T.Push(%#v), n = %s: Alloc/n = %.2f\n", v, c, s(memTestN), float64(n)/memTestN)
}


func BenchmarkVectorNums(b *testing.B) {
	c := int(0)
	var v Vector
	b.StopTimer()
	runtime.GC()
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		v.Push(c)
	}
}


func BenchmarkIntVectorNums(b *testing.B) {
	c := int(0)
	var v IntVector
	b.StopTimer()
	runtime.GC()
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		v.Push(c)
	}
}


func BenchmarkStringVectorNums(b *testing.B) {
	c := ""
	var v StringVector
	b.StopTimer()
	runtime.GC()
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		v.Push(c)
	}
}