Skip to content

Commit 150bcb0

Browse files
[benchmark] Add integer parsing benchmarks
1 parent d0ae1d9 commit 150bcb0

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-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: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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: "ParseIntFromDecimal",
17+
runFunction: run_ParseIntFromDecimal,
18+
tags: [.validation, .api],
19+
setUpFunction: { blackHole(decimalStrings) }),
20+
BenchmarkInfo(name: "ParseIntFromBinary",
21+
runFunction: run_ParseIntFromBinary,
22+
tags: [.validation, .api],
23+
setUpFunction: { blackHole(binaryStrings) }),
24+
BenchmarkInfo(name: "ParseIntFromHex",
25+
runFunction: run_ParseIntFromHex,
26+
tags: [.validation, .api],
27+
setUpFunction: { blackHole(hexStrings) }),
28+
BenchmarkInfo(name: "ParseIntFromUncommonRadix",
29+
runFunction: run_ParseIntFromUncommonRadix,
30+
tags: [.validation, .api],
31+
setUpFunction: { blackHole(uncommonRadixStrings) }),
32+
]
33+
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+
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) }
42+
43+
@inline(never)
44+
public func run_ParseIntFromDecimal(N: Int) {
45+
var result = 0
46+
for _ in 0..<N {
47+
for string in decimalStrings {
48+
result &+= Int(string, radix: 10)!
49+
}
50+
}
51+
blackHole(result)
52+
}
53+
54+
@inline(never)
55+
public func run_ParseIntFromBinary(N: Int) {
56+
var result = 0
57+
for _ in 0..<N {
58+
for string in binaryStrings {
59+
result &+= Int(string, radix: 2)!
60+
}
61+
}
62+
blackHole(result)
63+
}
64+
65+
@inline(never)
66+
public func run_ParseIntFromHex(N: Int) {
67+
var result = 0
68+
for _ in 0..<N {
69+
for string in hexStrings {
70+
result &+= Int(string, radix: 16)!
71+
}
72+
}
73+
blackHole(result)
74+
}
75+
76+
@inline(never)
77+
public func run_ParseIntFromUncommonRadix(N: Int) {
78+
var result = 0
79+
for _ in 0..<N {
80+
for string in uncommonRadixStrings {
81+
result &+= Int(string, radix: uncommonRadix)!
82+
}
83+
}
84+
blackHole(result)
85+
}

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)