OSDN Git Service

d540ace0502140450c98bcb4c0a44182cf3ff5cd
[pf3gnuchains/gcc-fork.git] / libgo / go / container / vector / numbers_test.go
1 // Copyright 2009 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.
4
5 package vector
6
7 import (
8         "fmt"
9         "runtime"
10         "strings"
11         "testing"
12 )
13
14
15 const memTestN = 1000000
16
17
18 func s(n uint64) string {
19         str := fmt.Sprintf("%d", n)
20         lens := len(str)
21         a := make([]string, (lens+2)/3)
22         start := lens
23         for i := range a {
24                 start -= 3
25                 if start < 0 {
26                         start = 0
27                 }
28                 a[len(a)-i-1] = str[start:lens]
29                 lens -= 3
30         }
31         return strings.Join(a, " ")
32 }
33
34
35 func TestVectorNums(t *testing.T) {
36         var v Vector
37         c := int(0)
38         runtime.GC()
39         m0 := runtime.MemStats
40         v.Resize(memTestN, memTestN)
41         for i := 0; i < memTestN; i++ {
42                 v.Set(i, c)
43         }
44         runtime.GC()
45         m := runtime.MemStats
46         v.Resize(0, 0)
47         runtime.GC()
48         n := m.Alloc - m0.Alloc
49         t.Logf("%T.Push(%#v), n = %s: Alloc/n = %.2f\n", v, c, s(memTestN), float64(n)/memTestN)
50 }
51
52
53 func TestIntVectorNums(t *testing.T) {
54         var v IntVector
55         c := int(0)
56         runtime.GC()
57         m0 := runtime.MemStats
58         v.Resize(memTestN, memTestN)
59         for i := 0; i < memTestN; i++ {
60                 v.Set(i, c)
61         }
62         runtime.GC()
63         m := runtime.MemStats
64         v.Resize(0, 0)
65         runtime.GC()
66         n := m.Alloc - m0.Alloc
67         t.Logf("%T.Push(%#v), n = %s: Alloc/n = %.2f\n", v, c, s(memTestN), float64(n)/memTestN)
68 }
69
70
71 func TestStringVectorNums(t *testing.T) {
72         var v StringVector
73         c := ""
74         runtime.GC()
75         m0 := runtime.MemStats
76         v.Resize(memTestN, memTestN)
77         for i := 0; i < memTestN; i++ {
78                 v.Set(i, c)
79         }
80         runtime.GC()
81         m := runtime.MemStats
82         v.Resize(0, 0)
83         runtime.GC()
84         n := m.Alloc - m0.Alloc
85         t.Logf("%T.Push(%#v), n = %s: Alloc/n = %.2f\n", v, c, s(memTestN), float64(n)/memTestN)
86 }
87
88
89 func BenchmarkVectorNums(b *testing.B) {
90         c := int(0)
91         var v Vector
92         b.StopTimer()
93         runtime.GC()
94         b.StartTimer()
95         for i := 0; i < b.N; i++ {
96                 v.Push(c)
97         }
98 }
99
100
101 func BenchmarkIntVectorNums(b *testing.B) {
102         c := int(0)
103         var v IntVector
104         b.StopTimer()
105         runtime.GC()
106         b.StartTimer()
107         for i := 0; i < b.N; i++ {
108                 v.Push(c)
109         }
110 }
111
112
113 func BenchmarkStringVectorNums(b *testing.B) {
114         c := ""
115         var v StringVector
116         b.StopTimer()
117         runtime.GC()
118         b.StartTimer()
119         for i := 0; i < b.N; i++ {
120                 v.Push(c)
121         }
122 }