Skip to content

Commit 414d860

Browse files
[benchmark] Split ParseInt into small/large & check results
1 parent b4654bf commit 414d860

File tree

1 file changed

+109
-31
lines changed

1 file changed

+109
-31
lines changed

benchmark/single-source/IntegerParsing.swift

Lines changed: 109 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,73 +13,151 @@
1313
import TestsUtils
1414

1515
public let IntegerParsing = [
16-
BenchmarkInfo(name: "ParseInt.Decimal",
17-
runFunction: run_ParseIntFromDecimal,
16+
BenchmarkInfo(name: "ParseInt.Small.Decimal",
17+
runFunction: run_ParseIntFromSmallDecimal,
1818
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,
2222
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,
2626
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,
3030
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) }),
3248
]
3349

34-
private let values: [Int] = Array(-1000...1000) // Give extra weight to low ints
35-
+ (0..<2000).map { _ in Int.random(in: .min ... .max) }
3650
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) }
4276

4377
@inline(never)
44-
public func run_ParseIntFromDecimal(N: Int) {
78+
public func run_ParseIntFromSmallDecimal(N: Int) {
4579
var result = 0
4680
for _ in 0..<N {
47-
for string in decimalStrings {
81+
for string in smallDecimalStrings {
4882
result &+= Int(string, radix: 10)!
4983
}
5084
}
51-
blackHole(result)
85+
CheckResults(result == smallValuesSum &* N)
5286
}
5387

5488
@inline(never)
55-
public func run_ParseIntFromBinary(N: Int) {
89+
public func run_ParseIntFromSmallBinary(N: Int) {
5690
var result = 0
5791
for _ in 0..<N {
58-
for string in binaryStrings {
92+
for string in smallBinaryStrings {
5993
result &+= Int(string, radix: 2)!
6094
}
6195
}
62-
blackHole(result)
96+
CheckResults(result == smallValuesSum &* N)
6397
}
6498

6599
@inline(never)
66-
public func run_ParseIntFromHex(N: Int) {
100+
public func run_ParseIntFromSmallHex(N: Int) {
67101
var result = 0
68102
for _ in 0..<N {
69-
for string in hexStrings {
103+
for string in smallHexStrings {
70104
result &+= Int(string, radix: 16)!
71105
}
72106
}
73-
blackHole(result)
107+
CheckResults(result == smallValuesSum &* N)
74108
}
75109

76110
@inline(never)
77-
public func run_ParseIntFromUncommonRadix(N: Int) {
111+
public func run_ParseIntFromSmallUncommonRadix(N: Int) {
78112
var result = 0
79113
for _ in 0..<N {
80-
for string in uncommonRadixStrings {
114+
for string in smallUncommonRadixStrings {
81115
result &+= Int(string, radix: uncommonRadix)!
82116
}
83117
}
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))
85163
}

0 commit comments

Comments
 (0)