|
17 | 17 |
|
18 | 18 | import TestsUtils
|
19 | 19 |
|
20 |
| -public let DictionaryCopy = [ |
21 |
| - BenchmarkInfo(name: "DictionaryCopy", runFunction: run_DictionaryCopy, |
22 |
| - tags: [.validation, .api, .Dictionary]), |
23 |
| - BenchmarkInfo(name: "DictionaryFilter", runFunction: run_DictionaryFilter, |
24 |
| - tags: [.validation, .api, .Dictionary]), |
25 |
| -] |
| 20 | +let t: [BenchmarkCategory] = [.validation, .api, .Dictionary] |
26 | 21 |
|
27 |
| -@inline(never) |
28 |
| -public func testCopy(_ size: Int) { |
29 |
| - var dict1 = [Int: Int](minimumCapacity: size) |
| 22 | +// We run the test at a spread of sizes between 1*x and 2*x, because the |
| 23 | +// quadratic behavior only happens at certain load factors. |
30 | 24 |
|
31 |
| - // Fill dictionary |
32 |
| - for i in 1...size { |
33 |
| - dict1[i] = 2 * i |
34 |
| - } |
35 |
| - CheckResults(dict1.count == size) |
| 25 | +public let DictionaryCopy = [ |
| 26 | + BenchmarkInfo(name:"Dict.CopyKeyValue.16k", |
| 27 | + runFunction: copyKeyValue, tags: t, setUpFunction: { dict(16_000) }), |
| 28 | + BenchmarkInfo(name:"Dict.CopyKeyValue.20k", |
| 29 | + runFunction: copyKeyValue, tags: t, setUpFunction: { dict(20_000) }), |
| 30 | + BenchmarkInfo(name:"Dict.CopyKeyValue.24k", |
| 31 | + runFunction: copyKeyValue, tags: t, setUpFunction: { dict(24_000) }), |
| 32 | + BenchmarkInfo(name:"Dict.CopyKeyValue.28k", |
| 33 | + runFunction: copyKeyValue, tags: t, setUpFunction: { dict(28_000) }), |
36 | 34 |
|
37 |
| - var dict2 = [Int: Int]() |
38 |
| - for (key, value) in dict1 { |
39 |
| - dict2[key] = value |
40 |
| - } |
41 |
| - CheckResults(dict2.count == size) |
42 |
| -} |
| 35 | + BenchmarkInfo(name:"Dict.FilterAllMatch.16k", |
| 36 | + runFunction: filterAllMatch, tags: t, setUpFunction: { dict(16_000) }), |
| 37 | + BenchmarkInfo(name:"Dict.FilterAllMatch.20k", |
| 38 | + runFunction: filterAllMatch, tags: t, setUpFunction: { dict(20_000) }), |
| 39 | + BenchmarkInfo(name:"Dict.FilterAllMatch.24k", |
| 40 | + runFunction: filterAllMatch, tags: t, setUpFunction: { dict(24_000) }), |
| 41 | + BenchmarkInfo(name:"Dict.FilterAllMatch.28k", |
| 42 | + runFunction: filterAllMatch, tags: t, setUpFunction: { dict(28_000) }), |
| 43 | +] |
43 | 44 |
|
44 |
| -@inline(never) |
45 |
| -public func run_DictionaryCopy(_ N: Int) { |
46 |
| - for _ in 0 ..< N { |
47 |
| - // We run the test at a spread of sizes between 1*x and 2*x, because the |
48 |
| - // quadratic behavior only happens at certain load factors. |
49 |
| - testCopy(100_000) |
50 |
| - testCopy(125_000) |
51 |
| - testCopy(150_000) |
52 |
| - testCopy(175_000) |
53 |
| - } |
| 45 | +var dict: [Int: Int]? |
| 46 | + |
| 47 | +func dict(_ size: Int) { |
| 48 | + dict = Dictionary(uniqueKeysWithValues: zip(1...size, 1...size)) |
54 | 49 | }
|
55 | 50 |
|
56 | 51 | @inline(never)
|
57 |
| -public func testFilter(_ size: Int) { |
58 |
| - var dict1 = [Int: Int](minimumCapacity: size) |
59 |
| - |
60 |
| - // Fill dictionary |
61 |
| - for i in 1...size { |
62 |
| - dict1[i] = 2 * i |
| 52 | +func copyKeyValue(N: Int) { |
| 53 | + for _ in 1...N { |
| 54 | + var copy = [Int: Int]() |
| 55 | + for (key, value) in dict! { |
| 56 | + copy[key] = value |
| 57 | + } |
| 58 | + CheckResults(copy.count == dict!.count) |
63 | 59 | }
|
64 |
| - CheckResults(dict1.count == size) |
65 |
| - |
66 |
| - // Filter with a predicate returning true is essentially the same loop as the |
67 |
| - // one in testCopy above. |
68 |
| - let dict2 = dict1.filter { _ in true } |
69 |
| - CheckResults(dict2.count == size) |
70 | 60 | }
|
71 | 61 |
|
| 62 | +// Filter with a predicate returning true is essentially the same loop as the |
| 63 | +// one in copyKeyValue above. |
72 | 64 | @inline(never)
|
73 |
| -public func run_DictionaryFilter(_ N: Int) { |
74 |
| - for _ in 0 ..< N { |
75 |
| - // We run the test at a spread of sizes between 1*x and 2*x, because the |
76 |
| - // quadratic behavior only happens at certain load factors. |
77 |
| - testFilter(100_000) |
78 |
| - testFilter(125_000) |
79 |
| - testFilter(150_000) |
80 |
| - testFilter(175_000) |
| 65 | +func filterAllMatch(N: Int) { |
| 66 | + for _ in 1...N { |
| 67 | + let copy = dict!.filter { _ in true } |
| 68 | + CheckResults(copy.count == dict!.count) |
81 | 69 | }
|
82 | 70 | }
|
0 commit comments