|
12 | 12 |
|
13 | 13 | import TestsUtils
|
14 | 14 |
|
| 15 | +// |
15 | 16 | // Mini benchmark implementing roman numeral conversions to/from integers.
|
16 |
| -// Measures performance of Substring.starts(with:), dropFirst and String.append |
| 17 | +// Measures performance of Substring.starts(with:) and String.append(), |
17 | 18 | // with very short string arguments.
|
18 |
| - |
19 |
| -let t: [BenchmarkCategory] = [.api, .String, .algorithm] |
20 |
| -let N = 270 |
| 19 | +// |
21 | 20 |
|
22 | 21 | public let RomanNumbers = [
|
23 | 22 | BenchmarkInfo(
|
24 |
| - name: "Roman.Substring.startsWith.dropFirst", |
25 |
| - runFunction: { |
26 |
| - checkId($0, upTo: N, { $0.romanNumeral }, Int.init(romanSSsWdF:)) }, |
27 |
| - tags: t), |
| 23 | + name: "RomanNumbers", |
| 24 | + runFunction: run_RomanNumbers, |
| 25 | + tags: [.api, .String, .algorithm]) |
28 | 26 | ]
|
29 | 27 |
|
30 |
| -@inline(__always) |
31 |
| -func checkId(_ n: Int, upTo limit: Int, _ itor: (Int) -> String, |
32 |
| - _ rtoi: (String) -> Int?) { |
33 |
| - for _ in 1...n { |
34 |
| - CheckResults( |
35 |
| - zip(1...limit, (1...limit).map(itor).map(rtoi)).allSatisfy { $0 == $1 }) |
36 |
| - } |
37 |
| -} |
38 |
| - |
39 | 28 | let romanTable: KeyValuePairs<String, Int> = [
|
40 |
| - "M": 1000, "CM": 900, "D": 500, "CD": 400, |
41 |
| - "C": 100_, "XC": 90_, "L": 50_, "XL": 40_, |
42 |
| - "X": 10__, "IX": 9__, "V": 5__, "IV": 4__, |
| 29 | + "M": 1000, |
| 30 | + "CM": 900, |
| 31 | + "D": 500, |
| 32 | + "CD": 400, |
| 33 | + "C": 100, |
| 34 | + "XC": 90, |
| 35 | + "L": 50, |
| 36 | + "XL": 40, |
| 37 | + "X": 10, |
| 38 | + "IX": 9, |
| 39 | + "V": 5, |
| 40 | + "IV": 4, |
43 | 41 | "I": 1,
|
44 | 42 | ]
|
45 | 43 |
|
46 | 44 | extension BinaryInteger {
|
47 |
| - // Imperative Style |
48 |
| - // See https://www.rosettacode.org/wiki/Roman_numerals/Encode#Swift |
49 |
| - // See https://www.rosettacode.org/wiki/Roman_numerals/Decode#Swift |
50 |
| - |
51 | 45 | var romanNumeral: String {
|
52 | 46 | var result = ""
|
53 |
| - var n = self |
54 |
| - for (numeral, value) in romanTable { |
55 |
| - while n >= value { |
56 |
| - result += numeral |
57 |
| - n -= Self(value) |
| 47 | + var value = self |
| 48 | + outer: |
| 49 | + while value > 0 { |
| 50 | + var position = 0 |
| 51 | + for (i, (key: s, value: v)) in romanTable[position...].enumerated() { |
| 52 | + if value >= v { |
| 53 | + result += s |
| 54 | + value -= Self(v) |
| 55 | + position = i |
| 56 | + continue outer |
| 57 | + } |
58 | 58 | }
|
| 59 | + fatalError("Unreachable") |
59 | 60 | }
|
60 | 61 | return result
|
61 | 62 | }
|
62 | 63 |
|
63 |
| - init?(romanSSsWdF number: String) { |
| 64 | + init?(romanNumeral: String) { |
64 | 65 | self = 0
|
65 |
| - var raw = Substring(number) |
66 |
| - for (numeral, value) in romanTable { |
67 |
| - while raw.starts(with: numeral) { |
68 |
| - self += Self(value) |
69 |
| - raw = raw.dropFirst(numeral.count) |
| 66 | + var input = Substring(romanNumeral) |
| 67 | + outer: |
| 68 | + while !input.isEmpty { |
| 69 | + var position = 0 |
| 70 | + for (i, (key: s, value: v)) in romanTable[position...].enumerated() { |
| 71 | + if input.starts(with: s) { |
| 72 | + self += Self(v) |
| 73 | + input = input.dropFirst(s.count) |
| 74 | + position = i |
| 75 | + continue outer |
| 76 | + } |
70 | 77 | }
|
| 78 | + return nil |
71 | 79 | }
|
72 |
| - guard raw.isEmpty else { return nil } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +@inline(never) |
| 84 | +func checkRomanNumerals(upTo limit: Int) { |
| 85 | + for i in 0 ..< limit { |
| 86 | + CheckResults(Int(romanNumeral: identity(i.romanNumeral)) == i) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +@inline(never) |
| 91 | +public func run_RomanNumbers(_ N: Int) { |
| 92 | + for _ in 0 ..< 10 * N { |
| 93 | + checkRomanNumerals(upTo: 1100) |
73 | 94 | }
|
74 | 95 | }
|
0 commit comments