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