Source file
test/maplinear.go
1
2
3
4
5
6
7
8
9
10
11 package main
12
13 import (
14 "fmt"
15 "math"
16 "time"
17 )
18
19
20
21 func checkLinear(typ string, tries int, f func(n int)) {
22
23
24
25
26
27 timeF := func(n int) time.Duration {
28 t1 := time.Now()
29 f(n)
30 return time.Since(t1)
31 }
32
33 t0 := time.Now()
34
35 n := tries
36 fails := 0
37 for {
38 t1 := timeF(n)
39 t2 := timeF(2 * n)
40
41
42 if t2 < 3*t1 {
43 if false {
44 fmt.Println(typ, "\t", time.Since(t0))
45 }
46 return
47 }
48
49
50
51
52 if t1 < 1*time.Second {
53 n *= 2
54 continue
55 }
56
57
58
59 if fails++; fails >= 5 {
60 panic(fmt.Sprintf("%s: too slow: %d inserts: %v; %d inserts: %v\n",
61 typ, n, t1, 2*n, t2))
62 }
63 }
64 }
65
66 type I interface {
67 f()
68 }
69
70 type C int
71
72 func (C) f() {}
73
74 func main() {
75
76 checkLinear("NaN", 30000, func(n int) {
77 m := map[float64]int{}
78 nan := math.NaN()
79 for i := 0; i < n; i++ {
80 m[nan] = 1
81 }
82 if len(m) != n {
83 panic("wrong size map after nan insertion")
84 }
85 })
86
87
88 checkLinear("eface", 10000, func(n int) {
89 m := map[interface{}]int{}
90 for i := 0; i < n; i++ {
91 m[i] = 1
92 }
93 })
94
95
96
97 checkLinear("iface", 10000, func(n int) {
98 m := map[I]int{}
99 for i := 0; i < n; i++ {
100 m[C(i)] = 1
101 }
102 })
103
104
105 checkLinear("int", 10000, func(n int) {
106 m := map[int]int{}
107 for i := 0; i < n; i++ {
108 m[i] = 1
109 }
110 })
111
112
113 checkLinear("string", 10000, func(n int) {
114 m := map[string]int{}
115 for i := 0; i < n; i++ {
116 m[fmt.Sprint(i)] = 1
117 }
118 })
119
120
121 checkLinear("float32", 10000, func(n int) {
122 m := map[float32]int{}
123 for i := 0; i < n; i++ {
124 m[float32(i)] = 1
125 }
126 })
127
128
129 checkLinear("float64", 10000, func(n int) {
130 m := map[float64]int{}
131 for i := 0; i < n; i++ {
132 m[float64(i)] = 1
133 }
134 })
135
136
137 checkLinear("complex64", 10000, func(n int) {
138 m := map[complex64]int{}
139 for i := 0; i < n; i++ {
140 m[complex(float32(i), float32(i))] = 1
141 }
142 })
143
144
145 checkLinear("complex128", 10000, func(n int) {
146 m := map[complex128]int{}
147 for i := 0; i < n; i++ {
148 m[complex(float64(i), float64(i))] = 1
149 }
150 })
151
152
153
154
155
156
157
158
159 checkLinear("iterdelete", 2500, func(n int) {
160 for round := 0; round < 4; round++ {
161 m := map[int]int{}
162 for i := 0; i < n; i++ {
163 m[i] = i
164 }
165 for i := 0; i < n; i++ {
166 for k := range m {
167 delete(m, k)
168 break
169 }
170 }
171 }
172 })
173 }
174
View as plain text