|
| 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 | +} |
0 commit comments