Skip to content

Commit 7c49272

Browse files
Merge pull request swiftlang#24476 from PatrickPijnappel/add-parsing-benchmark
[benchmark] Add integer parsing benchmarks
2 parents ca0205d + aa39122 commit 7c49272

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ set(SWIFT_BENCH_MODULES
9292
single-source/Hash
9393
single-source/Histogram
9494
single-source/InsertCharacter
95+
single-source/IntegerParsing
9596
single-source/Integrate
9697
single-source/IterateData
9798
single-source/Join
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
//===--- IntegerParsing.swift -------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 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+
13+
import TestsUtils
14+
15+
public let IntegerParsing = [
16+
BenchmarkInfo(name: "ParseInt.Small.Decimal",
17+
runFunction: run_ParseIntFromSmallDecimal,
18+
tags: [.validation, .api],
19+
setUpFunction: { blackHole(smallDecimalStrings) }),
20+
BenchmarkInfo(name: "ParseInt.Small.Binary",
21+
runFunction: run_ParseIntFromSmallBinary,
22+
tags: [.validation, .api],
23+
setUpFunction: { blackHole(smallBinaryStrings) }),
24+
BenchmarkInfo(name: "ParseInt.Small.Hex",
25+
runFunction: run_ParseIntFromSmallHex,
26+
tags: [.validation, .api],
27+
setUpFunction: { blackHole(smallHexStrings) }),
28+
BenchmarkInfo(name: "ParseInt.Small.UncommonRadix",
29+
runFunction: run_ParseIntFromSmallUncommonRadix,
30+
tags: [.validation, .api],
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) }),
48+
]
49+
50+
private let uncommonRadix: Int = 7
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) }
76+
77+
@inline(never)
78+
public func run_ParseIntFromSmallDecimal(N: Int) {
79+
var result = 0
80+
let count = N*10
81+
for _ in 0..<count {
82+
for string in smallDecimalStrings {
83+
result &+= Int(string, radix: 10)!
84+
}
85+
}
86+
CheckResults(result == smallValuesSum &* count)
87+
}
88+
89+
@inline(never)
90+
public func run_ParseIntFromSmallBinary(N: Int) {
91+
var result = 0
92+
let count = N*10
93+
for _ in 0..<count {
94+
for string in smallBinaryStrings {
95+
result &+= Int(string, radix: 2)!
96+
}
97+
}
98+
CheckResults(result == smallValuesSum &* count)
99+
}
100+
101+
@inline(never)
102+
public func run_ParseIntFromSmallHex(N: Int) {
103+
var result = 0
104+
let count = N*10
105+
for _ in 0..<count {
106+
for string in smallHexStrings {
107+
result &+= Int(string, radix: 16)!
108+
}
109+
}
110+
CheckResults(result == smallValuesSum &* count)
111+
}
112+
113+
@inline(never)
114+
public func run_ParseIntFromSmallUncommonRadix(N: Int) {
115+
var result = 0
116+
let count = N*10
117+
for _ in 0..<count {
118+
for string in smallUncommonRadixStrings {
119+
result &+= Int(string, radix: uncommonRadix)!
120+
}
121+
}
122+
CheckResults(result == smallValuesSum &* count)
123+
}
124+
125+
@inline(never)
126+
public func run_ParseIntFromLargeDecimal(N: Int) {
127+
var result: Int64 = 0
128+
for _ in 0..<N {
129+
for string in largeDecimalStrings {
130+
result &+= Int64(string, radix: 10)!
131+
}
132+
}
133+
CheckResults(result == largeValuesSum &* Int64(N))
134+
}
135+
136+
@inline(never)
137+
public func run_ParseIntFromLargeBinary(N: Int) {
138+
var result: Int64 = 0
139+
for _ in 0..<N {
140+
for string in largeBinaryStrings {
141+
result &+= Int64(string, radix: 2)!
142+
}
143+
}
144+
CheckResults(result == largeValuesSum &* Int64(N))
145+
}
146+
147+
@inline(never)
148+
public func run_ParseIntFromLargeHex(N: Int) {
149+
var result: Int64 = 0
150+
for _ in 0..<N {
151+
for string in largeHexStrings {
152+
result &+= Int64(string, radix: 16)!
153+
}
154+
}
155+
CheckResults(result == largeValuesSum &* Int64(N))
156+
}
157+
158+
@inline(never)
159+
public func run_ParseIntFromLargeUncommonRadix(N: Int) {
160+
var result: Int64 = 0
161+
for _ in 0..<N {
162+
for string in largeUncommonRadixStrings {
163+
result &+= Int64(string, radix: uncommonRadix)!
164+
}
165+
}
166+
CheckResults(result == largeValuesSum &* Int64(N))
167+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ import Hanoi
8080
import Hash
8181
import Histogram
8282
import InsertCharacter
83+
import IntegerParsing
8384
import Integrate
8485
import IterateData
8586
import Join
@@ -253,6 +254,7 @@ registerBenchmark(Hanoi)
253254
registerBenchmark(HashTest)
254255
registerBenchmark(Histogram)
255256
registerBenchmark(InsertCharacter)
257+
registerBenchmark(IntegerParsing)
256258
registerBenchmark(IntegrateTest)
257259
registerBenchmark(IterateData)
258260
registerBenchmark(Join)

0 commit comments

Comments
 (0)