OLD | NEW |
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 NaNs in maps don't go quadratic. | 8 // Test that NaNs in maps don't go quadratic. |
9 | 9 |
10 package main | 10 package main |
11 | 11 |
12 import ( | 12 import ( |
13 "fmt" | 13 "fmt" |
14 "math" | 14 "math" |
15 "time" | 15 "time" |
16 "syscall" | |
17 ) | 16 ) |
18 | 17 |
19 func main() { | 18 func main() { |
20 | 19 |
21 // Test that NaNs in maps don't go quadratic. | 20 // Test that NaNs in maps don't go quadratic. |
22 t := func(n int) time.Duration { | 21 t := func(n int) time.Duration { |
23 » » var u0 syscall.Rusage | 22 » » t1 := time.Now() |
24 » » if err := syscall.Getrusage(0, &u0); err != nil { | |
25 » » » panic(err) | |
26 » » } | |
27 m := map[float64]int{} | 23 m := map[float64]int{} |
28 nan := math.NaN() | 24 nan := math.NaN() |
29 for i := 0; i < n; i++ { | 25 for i := 0; i < n; i++ { |
30 m[nan] = 1 | 26 m[nan] = 1 |
31 } | 27 } |
32 if len(m) != n { | 28 if len(m) != n { |
33 panic("wrong size map after nan insertion") | 29 panic("wrong size map after nan insertion") |
34 } | 30 } |
35 » » var u1 syscall.Rusage | 31 » » return time.Since(t1) |
36 » » if err := syscall.Getrusage(0, &u1); err != nil { | |
37 » » » panic(err) | |
38 » » } | |
39 » » return time.Duration(u1.Utime.Nano() - u0.Utime.Nano()) | |
40 } | 32 } |
41 | 33 |
42 // Depending on the machine and OS, this test might be too fast | 34 // Depending on the machine and OS, this test might be too fast |
43 // to measure with accurate enough granularity. On failure, | 35 // to measure with accurate enough granularity. On failure, |
44 // make it run longer, hoping that the timing granularity | 36 // make it run longer, hoping that the timing granularity |
45 // is eventually sufficient. | 37 // is eventually sufficient. |
46 | 38 |
47 n := 30000 // ~8ms user time on a Mid 2011 MacBook Air (1.8 GHz Core i7) | 39 n := 30000 // ~8ms user time on a Mid 2011 MacBook Air (1.8 GHz Core i7) |
48 fails := 0 | 40 fails := 0 |
49 for { | 41 for { |
50 t1 := t(n) | 42 t1 := t(n) |
51 t2 := t(2 * n) | 43 t2 := t(2 * n) |
52 // should be 2x (linear); allow up to 3x | 44 // should be 2x (linear); allow up to 3x |
53 if t2 < 3*t1 { | 45 if t2 < 3*t1 { |
54 return | 46 return |
55 } | 47 } |
56 fails++ | 48 fails++ |
57 if fails == 6 { | 49 if fails == 6 { |
58 panic(fmt.Sprintf("too slow: %d inserts: %v; %d inserts:
%v\n", n, t1, 2*n, t2)) | 50 panic(fmt.Sprintf("too slow: %d inserts: %v; %d inserts:
%v\n", n, t1, 2*n, t2)) |
59 } | 51 } |
60 if fails < 4 { | 52 if fails < 4 { |
61 n *= 2 | 53 n *= 2 |
62 } | 54 } |
63 } | 55 } |
64 } | 56 } |
OLD | NEW |