forked from pointfreeco/swift-parsing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumerics.swift
160 lines (143 loc) · 4.11 KB
/
Numerics.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import Benchmark
import Foundation
import Parsing
/// This benchmark demonstrates how the numeric parsers in the library compare to Apple's tools, such
/// as initializers and `Scanner`.
let numericsSuite = BenchmarkSuite(name: "Numerics") { suite in
do {
let input = "123"
let expected = 123
var output: Int!
suite.benchmark("Int.init") {
output = Int(input)
} tearDown: {
precondition(output == expected)
}
suite.benchmark("Int.parser") {
var input = input[...].utf8
output = try Int.parser().parse(&input)
} tearDown: {
precondition(output == expected)
}
if #available(macOS 10.15, *) {
let scanner = Scanner(string: input)
suite.benchmark("Scanner.scanInt") {
output = scanner.scanInt()
} setUp: {
scanner.currentIndex = input.startIndex
} tearDown: {
precondition(output == expected)
}
}
suite.benchmark("Digits") {
var input = input[...].utf8
output = try Digits().parse(&input)
} tearDown: {
precondition(output == expected)
}
}
do {
let input = (1...100_000).map(String.init).joined(separator: ",")
let expected = Array(1...100_000)
var output: [Int]!
suite.benchmark("Comma separated: Int.parser") {
var input = input[...].utf8
output = try Many {
Int.parser()
} separator: {
",".utf8
}
.parse(&input)
} tearDown: {
precondition(output == expected)
}
if #available(macOS 10.15, *) {
let scanner = Scanner(string: input)
suite.benchmark("Comma separated: Scanner.scanInt") {
output = []
while let n = scanner.scanInt() {
output.append(n)
guard let separator = scanner.scanCharacter() else { break }
guard separator == "," else {
scanner.string.formIndex(before: &scanner.currentIndex)
break
}
}
} setUp: {
scanner.currentIndex = input.startIndex
} tearDown: {
precondition(output == expected)
}
}
suite.benchmark("Comma separated: String.split") {
output = input.split(separator: ",").compactMap { Int($0) }
} tearDown: {
precondition(output == expected)
}
}
do {
let input = "123.45"
let expected = 123.45
var output: Double!
suite.benchmark("Double.init") {
output = Double(input)
} tearDown: {
precondition(output == expected)
}
suite.benchmark("Double.parser") {
var input = input[...].utf8
output = try Double.parser().parse(&input)
} tearDown: {
precondition(output == expected)
}
if #available(macOS 10.15, *) {
let scanner = Scanner(string: input)
suite.benchmark("Scanner.scanDouble") {
output = scanner.scanDouble()
} setUp: {
scanner.currentIndex = input.startIndex
} tearDown: {
precondition(output == expected)
}
}
}
do {
let input = (1...100_000).map(String.init).joined(separator: ",")
let expected = (1...100_000).map(Double.init)
var output: [Double]!
suite.benchmark("Comma separated: Double.parser") {
var input = input[...].utf8
output = try Many {
Double.parser()
} separator: {
",".utf8
}
.parse(&input)
} tearDown: {
precondition(output == expected)
}
if #available(macOS 10.15, *) {
let scanner = Scanner(string: input)
suite.benchmark("Comma separated: Scanner.scanDouble") {
output = []
while let n = scanner.scanDouble() {
output.append(n)
guard let separator = scanner.scanCharacter() else { break }
guard separator == "," else {
scanner.string.formIndex(before: &scanner.currentIndex)
break
}
}
} setUp: {
scanner.currentIndex = input.startIndex
} tearDown: {
precondition(output == expected)
}
}
suite.benchmark("Comma separated: String.split") {
output = input.split(separator: ",").compactMap { Double($0) }
} tearDown: {
precondition(output == expected)
}
}
}