LEFT | RIGHT |
1 // +build darwin linux | 1 // +build darwin linux |
2 // run | 2 // run |
3 | 3 |
4 // Copyright 2013 The Go Authors. All rights reserved. | 4 // Copyright 2013 The Go Authors. All rights reserved. |
5 // Use of this source code is governed by a BSD-style | 5 // Use of this source code is governed by a BSD-style |
6 // license that can be found in the LICENSE file. | 6 // license that can be found in the LICENSE file. |
7 | 7 |
8 // Test that maps don't go quadratic for NaNs and other values. | 8 // Test that maps don't go quadratic for NaNs and other values. |
9 | 9 |
10 package main | 10 package main |
(...skipping 21 matching lines...) Expand all Loading... |
32 t0 := time.Now() | 32 t0 := time.Now() |
33 | 33 |
34 n := tries | 34 n := tries |
35 fails := 0 | 35 fails := 0 |
36 for { | 36 for { |
37 t1 := timeF(n) | 37 t1 := timeF(n) |
38 t2 := timeF(2 * n) | 38 t2 := timeF(2 * n) |
39 | 39 |
40 // should be 2x (linear); allow up to 3x | 40 // should be 2x (linear); allow up to 3x |
41 if t2 < 3*t1 { | 41 if t2 < 3*t1 { |
42 » » » fmt.Println(typ, "\t", time.Since(t0)) | 42 » » » if false { |
| 43 » » » » fmt.Println(typ, "\t", time.Since(t0)) |
| 44 » » » } |
43 return | 45 return |
44 } | 46 } |
45 fails++ | 47 fails++ |
46 if fails == 6 { | 48 if fails == 6 { |
47 panic(fmt.Sprintf("%s: too slow: %d inserts: %v; %d inse
rts: %v\n", | 49 panic(fmt.Sprintf("%s: too slow: %d inserts: %v; %d inse
rts: %v\n", |
48 typ, n, t1, 2*n, t2)) | 50 typ, n, t1, 2*n, t2)) |
49 } | 51 } |
50 if fails < 4 { | 52 if fails < 4 { |
51 n *= 2 | 53 n *= 2 |
52 } | 54 } |
53 } | 55 } |
54 } | 56 } |
55 | 57 |
56 type I interface { | 58 type I interface { |
57 f() | 59 f() |
58 } | 60 } |
59 | 61 |
60 type C int | 62 type C int |
61 | 63 |
62 func (C) f() {} | 64 func (C) f() {} |
63 | 65 |
64 func main() { | 66 func main() { |
65 » // NaNs. ~8ms user time on a Mid 2011 MacBook Air (1.8 GHz Core i7)) | 67 » // NaNs. ~31ms on a 1.6GHz Zeon. |
66 » // ~31ms on a 1.6GHz Zeon. | |
67 checkLinear("NaN", 30000, func(n int) { | 68 checkLinear("NaN", 30000, func(n int) { |
68 m := map[float64]int{} | 69 m := map[float64]int{} |
69 nan := math.NaN() | 70 nan := math.NaN() |
70 for i := 0; i < n; i++ { | 71 for i := 0; i < n; i++ { |
71 m[nan] = 1 | 72 m[nan] = 1 |
72 } | 73 } |
73 if len(m) != n { | 74 if len(m) != n { |
74 panic("wrong size map after nan insertion") | 75 panic("wrong size map after nan insertion") |
75 } | 76 } |
76 }) | 77 }) |
(...skipping 24 matching lines...) Expand all Loading... |
101 }) | 102 }) |
102 | 103 |
103 // ~18ms on a 1.6GHz Zeon. | 104 // ~18ms on a 1.6GHz Zeon. |
104 checkLinear("string", 10000, func(n int) { | 105 checkLinear("string", 10000, func(n int) { |
105 m := map[string]int{} | 106 m := map[string]int{} |
106 for i := 0; i < n; i++ { | 107 for i := 0; i < n; i++ { |
107 m[fmt.Sprint(i)] = 1 | 108 m[fmt.Sprint(i)] = 1 |
108 } | 109 } |
109 }) | 110 }) |
110 | 111 |
111 » // The following checks seem to fail, for a variety of tries | 112 » // ~6ms on a 1.6GHz Zeon. |
112 » // values, because of non-linear behaviour. | 113 » checkLinear("float32", 10000, func(n int) { |
113 » if false { | 114 » » m := map[float32]int{} |
114 » » checkLinear("float32", 1000, func(n int) { | 115 » » for i := 0; i < n; i++ { |
115 » » » m := map[float32]int{} | 116 » » » m[float32(i)] = 1 |
116 » » » for i := 0; i < n; i++ { | 117 » » } |
117 » » » » m[float32(i)] = 1 | 118 » }) |
118 » » » } | |
119 » » }) | |
120 | 119 |
121 » » checkLinear("float64", 1000, func(n int) { | 120 » // ~6ms on a 1.6GHz Zeon. |
122 » » » m := map[float64]int{} | 121 » checkLinear("float64", 10000, func(n int) { |
123 » » » for i := 0; i < n; i++ { | 122 » » m := map[float64]int{} |
124 » » » » m[float64(i)] = 1 | 123 » » for i := 0; i < n; i++ { |
125 » » » } | 124 » » » m[float64(i)] = 1 |
126 » » }) | 125 » » } |
| 126 » }) |
127 | 127 |
128 » » checkLinear("complex64", 2000, func(n int) { | 128 » // ~22ms on a 1.6GHz Zeon. |
129 » » » m := map[complex64]int{} | 129 » checkLinear("complex64", 10000, func(n int) { |
130 » » » for i := 0; i < n; i++ { | 130 » » m := map[complex64]int{} |
131 » » » » m[complex(float32(i), float32(i))] = 1 | 131 » » for i := 0; i < n; i++ { |
132 » » » } | 132 » » » m[complex(float32(i), float32(i))] = 1 |
133 » » }) | 133 » » } |
| 134 » }) |
134 | 135 |
135 » » checkLinear("complex128", 2000, func(n int) { | 136 » // ~32ms on a 1.6GHz Zeon. |
136 » » » m := map[complex128]int{} | 137 » checkLinear("complex128", 10000, func(n int) { |
137 » » » for i := 0; i < n; i++ { | 138 » » m := map[complex128]int{} |
138 » » » » m[complex(float64(i), float64(i))] = 1 | 139 » » for i := 0; i < n; i++ { |
139 » » » } | 140 » » » m[complex(float64(i), float64(i))] = 1 |
140 » » }) | 141 » » } |
141 » } | 142 » }) |
142 } | 143 } |
LEFT | RIGHT |