Skip to content

Commit dff2747

Browse files
committed
FlattenSequence/distance(from:to:) benchmarks (v4).
1. Added some sequential tests with String as the inner collection.
1 parent 6829f80 commit dff2747

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

benchmark/single-source/FlattenDistanceFromTo.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,30 @@ public let FlattenDistanceFromTo = [
3636
runFunction: { with(randomAccess32x32, $0) },
3737
tags: [.validation, .api],
3838
setUpFunction: { blackHole(randomAccess32x32) }),
39+
40+
BenchmarkInfo(
41+
name: "FlattenDistanceFromTo.RepeatString.08x08",
42+
runFunction: { with(repeatString08x08, $0) },
43+
tags: [.validation, .api],
44+
setUpFunction: { blackHole(repeatString08x08) }),
45+
46+
BenchmarkInfo(
47+
name: "FlattenDistanceFromTo.RepeatString.08x16",
48+
runFunction: { with(repeatString08x16, $0) },
49+
tags: [.validation, .api],
50+
setUpFunction: { blackHole(repeatString08x16) }),
51+
52+
BenchmarkInfo(
53+
name: "FlattenDistanceFromTo.RepeatString.16x08",
54+
runFunction: { with(repeatString16x08, $0) },
55+
tags: [.validation, .api],
56+
setUpFunction: { blackHole(repeatString16x08) }),
57+
58+
BenchmarkInfo(
59+
name: "FlattenDistanceFromTo.RepeatString.16x16",
60+
runFunction: { with(repeatString16x16, $0) },
61+
tags: [.validation, .api],
62+
setUpFunction: { blackHole(repeatString16x16) }),
3963
]
4064

4165
// MARK: - Random Access
@@ -58,7 +82,40 @@ public func with(
5882
_ iterations: Int
5983
) {
6084
var value = 0 as Int
85+
86+
for _ in 0 ..< iterations {
87+
for a in collection.indices {
88+
for b in collection.indices {
89+
value &+= collection.distance(from: a, to: b)
90+
value &+= collection.distance(from: b, to: a)
91+
}
92+
}
93+
}
94+
95+
blackHole(value == 0)
96+
}
97+
98+
// MARK: - Repeat String
99+
100+
func makeRepeatString(
101+
_ outer: Int,
102+
_ inner: Int
103+
) -> FlattenSequence<Repeated<String>> {
104+
repeatElement(String(repeating: "0", count: inner), count: outer).joined()
105+
}
61106

107+
let repeatString08x08 = makeRepeatString(08, 08)
108+
let repeatString08x16 = makeRepeatString(08, 16)
109+
let repeatString16x08 = makeRepeatString(16, 08)
110+
let repeatString16x16 = makeRepeatString(16, 16)
111+
112+
@inline(never)
113+
public func with(
114+
_ collection: FlattenSequence<Repeated<String>>,
115+
_ iterations: Int
116+
) {
117+
var value = 0 as Int
118+
62119
for _ in 0 ..< iterations {
63120
for a in collection.indices {
64121
for b in collection.indices {

0 commit comments

Comments
 (0)