|
13 | 13 | import TestsUtils
|
14 | 14 |
|
15 | 15 | public let IntegerParsing = [
|
16 |
| - BenchmarkInfo(name: "ParseInt.Decimal", |
17 |
| - runFunction: run_ParseIntFromDecimal, |
| 16 | + BenchmarkInfo(name: "ParseInt.Small.Decimal", |
| 17 | + runFunction: run_ParseIntFromSmallDecimal, |
18 | 18 | tags: [.validation, .api],
|
19 |
| - setUpFunction: { blackHole(decimalStrings) }), |
20 |
| - BenchmarkInfo(name: "ParseInt.Binary", |
21 |
| - runFunction: run_ParseIntFromBinary, |
| 19 | + setUpFunction: { blackHole(smallDecimalStrings) }), |
| 20 | + BenchmarkInfo(name: "ParseInt.Small.Binary", |
| 21 | + runFunction: run_ParseIntFromSmallBinary, |
22 | 22 | tags: [.validation, .api],
|
23 |
| - setUpFunction: { blackHole(binaryStrings) }), |
24 |
| - BenchmarkInfo(name: "ParseInt.Hex", |
25 |
| - runFunction: run_ParseIntFromHex, |
| 23 | + setUpFunction: { blackHole(smallBinaryStrings) }), |
| 24 | + BenchmarkInfo(name: "ParseInt.Small.Hex", |
| 25 | + runFunction: run_ParseIntFromSmallHex, |
26 | 26 | tags: [.validation, .api],
|
27 |
| - setUpFunction: { blackHole(hexStrings) }), |
28 |
| - BenchmarkInfo(name: "ParseInt.UncommonRadix", |
29 |
| - runFunction: run_ParseIntFromUncommonRadix, |
| 27 | + setUpFunction: { blackHole(smallHexStrings) }), |
| 28 | + BenchmarkInfo(name: "ParseInt.Small.UncommonRadix", |
| 29 | + runFunction: run_ParseIntFromSmallUncommonRadix, |
30 | 30 | tags: [.validation, .api],
|
31 |
| - setUpFunction: { blackHole(uncommonRadixStrings) }), |
| 31 | + setUpFunction: { blackHole(smallUncommonRadixStrings) }), |
| 32 | + BenchmarkInfo(name: "ParseInt.Large.Decimal", |
| 33 | + runFunction: run_ParseIntFromLargeDecimal, |
| 34 | + tags: [.validation, .api], |
| 35 | + setUpFunction: { blackHole(largeDecimalStrings) }), |
| 36 | + BenchmarkInfo(name: "ParseInt.Large.Binary", |
| 37 | + runFunction: run_ParseIntFromLargeBinary, |
| 38 | + tags: [.validation, .api], |
| 39 | + setUpFunction: { blackHole(largeBinaryStrings) }), |
| 40 | + BenchmarkInfo(name: "ParseInt.Large.Hex", |
| 41 | + runFunction: run_ParseIntFromLargeHex, |
| 42 | + tags: [.validation, .api], |
| 43 | + setUpFunction: { blackHole(largeHexStrings) }), |
| 44 | + BenchmarkInfo(name: "ParseInt.Large.UncommonRadix", |
| 45 | + runFunction: run_ParseIntFromLargeUncommonRadix, |
| 46 | + tags: [.validation, .api], |
| 47 | + setUpFunction: { blackHole(largeUncommonRadixStrings) }), |
32 | 48 | ]
|
33 | 49 |
|
34 |
| -private let values: [Int] = Array(-1000...1000) // Give extra weight to low ints |
35 |
| - + (0..<2000).map { _ in Int.random(in: .min ... .max) } |
36 | 50 | private let uncommonRadix: Int = 7
|
37 |
| -private let decimalStrings: [String] = values.map { String($0, radix: 10) } |
38 |
| -private let binaryStrings: [String] = values.map { String($0, radix: 2) } |
39 |
| -private let hexStrings: [String] = values.map { String($0, radix: 16) } |
40 |
| -private let uncommonRadixStrings: [String] |
41 |
| - = values.map { String($0, radix: uncommonRadix) } |
| 51 | +private let smallValuesSum: Int = 0 |
| 52 | +private let largeValuesSum: Int64 = 4790606764485943206 |
| 53 | +// Values |
| 54 | +private let smallValues: [Int] = Array(-1000...1000) |
| 55 | +private let largeValues: [Int64] = { |
| 56 | + var rng = SplitMix64(seed: 42) |
| 57 | + return (0..<2000).map { _ in Int64.random(in: .min ... .max, using: &rng) } |
| 58 | +}() |
| 59 | +// Strings |
| 60 | +private let smallDecimalStrings: [String] |
| 61 | + = smallValues.map { String($0, radix: 10) } |
| 62 | +private let smallBinaryStrings: [String] |
| 63 | + = smallValues.map { String($0, radix: 2) } |
| 64 | +private let smallHexStrings: [String] |
| 65 | + = smallValues.map { String($0, radix: 16) } |
| 66 | +private let smallUncommonRadixStrings: [String] |
| 67 | + = smallValues.map { String($0, radix: uncommonRadix) } |
| 68 | +private let largeDecimalStrings: [String] |
| 69 | + = largeValues.map { String($0, radix: 10) } |
| 70 | +private let largeBinaryStrings: [String] |
| 71 | + = largeValues.map { String($0, radix: 2) } |
| 72 | +private let largeHexStrings: [String] |
| 73 | + = largeValues.map { String($0, radix: 16) } |
| 74 | +private let largeUncommonRadixStrings: [String] |
| 75 | + = largeValues.map { String($0, radix: uncommonRadix) } |
42 | 76 |
|
43 | 77 | @inline(never)
|
44 |
| -public func run_ParseIntFromDecimal(N: Int) { |
| 78 | +public func run_ParseIntFromSmallDecimal(N: Int) { |
45 | 79 | var result = 0
|
46 | 80 | for _ in 0..<N {
|
47 |
| - for string in decimalStrings { |
| 81 | + for string in smallDecimalStrings { |
48 | 82 | result &+= Int(string, radix: 10)!
|
49 | 83 | }
|
50 | 84 | }
|
51 |
| - blackHole(result) |
| 85 | + CheckResults(result == smallValuesSum &* N) |
52 | 86 | }
|
53 | 87 |
|
54 | 88 | @inline(never)
|
55 |
| -public func run_ParseIntFromBinary(N: Int) { |
| 89 | +public func run_ParseIntFromSmallBinary(N: Int) { |
56 | 90 | var result = 0
|
57 | 91 | for _ in 0..<N {
|
58 |
| - for string in binaryStrings { |
| 92 | + for string in smallBinaryStrings { |
59 | 93 | result &+= Int(string, radix: 2)!
|
60 | 94 | }
|
61 | 95 | }
|
62 |
| - blackHole(result) |
| 96 | + CheckResults(result == smallValuesSum &* N) |
63 | 97 | }
|
64 | 98 |
|
65 | 99 | @inline(never)
|
66 |
| -public func run_ParseIntFromHex(N: Int) { |
| 100 | +public func run_ParseIntFromSmallHex(N: Int) { |
67 | 101 | var result = 0
|
68 | 102 | for _ in 0..<N {
|
69 |
| - for string in hexStrings { |
| 103 | + for string in smallHexStrings { |
70 | 104 | result &+= Int(string, radix: 16)!
|
71 | 105 | }
|
72 | 106 | }
|
73 |
| - blackHole(result) |
| 107 | + CheckResults(result == smallValuesSum &* N) |
74 | 108 | }
|
75 | 109 |
|
76 | 110 | @inline(never)
|
77 |
| -public func run_ParseIntFromUncommonRadix(N: Int) { |
| 111 | +public func run_ParseIntFromSmallUncommonRadix(N: Int) { |
78 | 112 | var result = 0
|
79 | 113 | for _ in 0..<N {
|
80 |
| - for string in uncommonRadixStrings { |
| 114 | + for string in smallUncommonRadixStrings { |
81 | 115 | result &+= Int(string, radix: uncommonRadix)!
|
82 | 116 | }
|
83 | 117 | }
|
84 |
| - blackHole(result) |
| 118 | + CheckResults(result == smallValuesSum &* N) |
| 119 | +} |
| 120 | + |
| 121 | +@inline(never) |
| 122 | +public func run_ParseIntFromLargeDecimal(N: Int) { |
| 123 | + var result: Int64 = 0 |
| 124 | + for _ in 0..<N { |
| 125 | + for string in largeDecimalStrings { |
| 126 | + result &+= Int64(string, radix: 10)! |
| 127 | + } |
| 128 | + } |
| 129 | + CheckResults(result == largeValuesSum &* Int64(N)) |
| 130 | +} |
| 131 | + |
| 132 | +@inline(never) |
| 133 | +public func run_ParseIntFromLargeBinary(N: Int) { |
| 134 | + var result: Int64 = 0 |
| 135 | + for _ in 0..<N { |
| 136 | + for string in largeBinaryStrings { |
| 137 | + result &+= Int64(string, radix: 2)! |
| 138 | + } |
| 139 | + } |
| 140 | + CheckResults(result == largeValuesSum &* Int64(N)) |
| 141 | +} |
| 142 | + |
| 143 | +@inline(never) |
| 144 | +public func run_ParseIntFromLargeHex(N: Int) { |
| 145 | + var result: Int64 = 0 |
| 146 | + for _ in 0..<N { |
| 147 | + for string in largeHexStrings { |
| 148 | + result &+= Int64(string, radix: 16)! |
| 149 | + } |
| 150 | + } |
| 151 | + CheckResults(result == largeValuesSum &* Int64(N)) |
| 152 | +} |
| 153 | + |
| 154 | +@inline(never) |
| 155 | +public func run_ParseIntFromLargeUncommonRadix(N: Int) { |
| 156 | + var result: Int64 = 0 |
| 157 | + for _ in 0..<N { |
| 158 | + for string in largeUncommonRadixStrings { |
| 159 | + result &+= Int64(string, radix: uncommonRadix)! |
| 160 | + } |
| 161 | + } |
| 162 | + CheckResults(result == largeValuesSum &* Int64(N)) |
85 | 163 | }
|
0 commit comments