|
1 |
| -// Dictionary compact map values benchmark |
| 1 | +//===--- DictionaryCompactMapValues.swift ---------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
2 | 13 | import TestsUtils
|
3 | 14 |
|
| 15 | +let size = 100 |
| 16 | +let oddNumbers = stride(from: 1, to: size, by: 2) |
| 17 | +let smallOddNumMap: [Int: Int?] = |
| 18 | + Dictionary(uniqueKeysWithValues: zip(oddNumbers, oddNumbers)) |
| 19 | +let compactOddNums: [Int: Int] = |
| 20 | + Dictionary(uniqueKeysWithValues: zip(oddNumbers, oddNumbers)) |
| 21 | +let oddStringMap: [Int: String] = Dictionary(uniqueKeysWithValues: |
| 22 | + (1...size).lazy.map { ($0, $0 % 2 == 0 ? "dummy" : "\($0)") }) |
| 23 | + |
| 24 | +let t: [BenchmarkCategory] = [.validation, .api, .Dictionary] |
| 25 | + |
4 | 26 | public let DictionaryCompactMapValues = [
|
5 | 27 | BenchmarkInfo(name: "DictionaryCompactMapValuesOfNilValue",
|
6 |
| - runFunction: run_DictionaryCompactMapValuesOfNilValue, |
7 |
| - tags: [.validation, .api, .Dictionary], |
| 28 | + runFunction: compactMapValues, tags: t, |
| 29 | + setUpFunction: { blackHole(smallOddNumMap); blackHole(compactOddNums)}, |
8 | 30 | legacyFactor: 50),
|
9 | 31 | BenchmarkInfo(name: "DictionaryCompactMapValuesOfCastValue",
|
10 |
| - runFunction: run_DictionaryCompactMapValuesOfCastValue, |
11 |
| - tags: [.validation, .api, .Dictionary], |
12 |
| - legacyFactor: 50), |
| 32 | + runFunction: compactMapValuesInt, tags: t, |
| 33 | + setUpFunction: { blackHole(oddStringMap); blackHole(compactOddNums)}, |
| 34 | + legacyFactor: 54), |
13 | 35 | ]
|
14 | 36 |
|
15 |
| -@inline(never) |
16 |
| -public func run_DictionaryCompactMapValuesOfNilValue(_ N: Int) { |
17 |
| - let size = 100 |
18 |
| - var dict = [Int: Int?](minimumCapacity: size) |
19 |
| - |
20 |
| - // Fill Dictionary |
21 |
| - for i in 1...size { |
22 |
| - if i % 2 == 0 { |
23 |
| - dict[i] = nil |
24 |
| - } else { |
25 |
| - dict[i] = i |
26 |
| - } |
27 |
| - } |
28 |
| - CheckResults(dict.count == size / 2) |
29 |
| - |
30 |
| - var refDict = [Int: Int]() |
31 |
| - for i in stride(from: 1, to: 100, by: 2) { |
32 |
| - refDict[i] = i |
33 |
| - } |
34 |
| - |
35 |
| - var newDict = [Int: Int]() |
| 37 | +func compactMapValues(N: Int) { |
36 | 38 | for _ in 1...20*N {
|
37 |
| - newDict = dict.compactMapValues({$0}) |
38 |
| - if newDict != refDict { |
39 |
| - break |
40 |
| - } |
| 39 | + CheckResults(smallOddNumMap.compactMapValues({$0}) == compactOddNums) |
41 | 40 | }
|
42 |
| - |
43 |
| - CheckResults(newDict == refDict) |
44 | 41 | }
|
45 | 42 |
|
46 |
| -@inline(never) |
47 |
| -public func run_DictionaryCompactMapValuesOfCastValue(_ N: Int) { |
48 |
| - let size = 100 |
49 |
| - var dict = [Int: String](minimumCapacity: size) |
50 |
| - |
51 |
| - // Fill Dictionary |
52 |
| - for i in 1...size { |
53 |
| - if i % 2 == 0 { |
54 |
| - dict[i] = "dummy" |
55 |
| - } else { |
56 |
| - dict[i] = "\(i)" |
57 |
| - } |
58 |
| - } |
59 |
| - |
60 |
| - CheckResults(dict.count == size) |
61 |
| - |
62 |
| - var refDict = [Int: Int]() |
63 |
| - for i in stride(from: 1, to: 100, by: 2) { |
64 |
| - refDict[i] = i |
65 |
| - } |
66 |
| - |
67 |
| - var newDict = [Int: Int]() |
| 43 | +func compactMapValuesInt(N: Int) { |
68 | 44 | for _ in 1...20*N {
|
69 |
| - newDict = dict.compactMapValues(Int.init) |
70 |
| - if newDict != refDict { |
71 |
| - break |
72 |
| - } |
| 45 | + CheckResults(oddStringMap.compactMapValues(Int.init) == compactOddNums) |
73 | 46 | }
|
74 |
| - |
75 |
| - CheckResults(newDict == refDict) |
76 | 47 | }
|
0 commit comments