Skip to content

Commit 384cb5a

Browse files
committed
[benchmark] RomanNumbers: Use DictionaryLiteral and enumerated().
1 parent 9e375bb commit 384cb5a

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

benchmark/single-source/RomanNumbers.swift

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ public let RomanNumbers = [
2525
tags: [.api, .String, .algorithm])
2626
]
2727

28-
let romanTable: [(Int, String)] = [
29-
(1000, "M"),
30-
(900, "CM"),
31-
(500, "D"),
32-
(400, "CD"),
33-
(100, "C"),
34-
(90, "XC"),
35-
(50, "L"),
36-
(40, "XL"),
37-
(10, "X"),
38-
(9, "IX"),
39-
(5, "V"),
40-
(4, "IV"),
41-
(1, "I"),
28+
let romanTable: DictionaryLiteral<String, Int> = [
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,
41+
"I": 1,
4242
]
4343

4444
extension BinaryInteger {
@@ -48,8 +48,7 @@ extension BinaryInteger {
4848
outer:
4949
while value > 0 {
5050
var position = 0
51-
for i in position ..< romanTable.count {
52-
let (v, s) = romanTable[i]
51+
for (i, (key: s, value: v)) in romanTable[position...].enumerated() {
5352
if value >= v {
5453
result += s
5554
value -= Self(v)
@@ -68,8 +67,7 @@ extension BinaryInteger {
6867
outer:
6968
while !input.isEmpty {
7069
var position = 0
71-
for i in position ..< romanTable.count {
72-
let (v, s) = romanTable[i]
70+
for (i, (key: s, value: v)) in romanTable[position...].enumerated() {
7371
if input.starts(with: s) {
7472
self += Self(v)
7573
input = input.dropFirst(s.count)

0 commit comments

Comments
 (0)