Skip to content

Commit 6829f80

Browse files
committed
FlattenSequence/distance(from:to:) benchmarks (v3).
1. Parameterized benchmark dependencies, per code review.
1 parent e6cc57f commit 6829f80

File tree

1 file changed

+37
-23
lines changed

1 file changed

+37
-23
lines changed

benchmark/single-source/FlattenDistanceFromTo.swift

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,58 @@ import TestsUtils
1515
public let FlattenDistanceFromTo = [
1616
BenchmarkInfo(
1717
name: "FlattenDistanceFromTo.RandomAccess.16x16",
18-
runFunction: { withRandomAccess(16, 16, $0) },
19-
tags: [.validation, .api]),
20-
18+
runFunction: { with(randomAccess16x16, $0) },
19+
tags: [.validation, .api],
20+
setUpFunction: { blackHole(randomAccess16x16) }),
21+
2122
BenchmarkInfo(
2223
name: "FlattenDistanceFromTo.RandomAccess.16x32",
23-
runFunction: { withRandomAccess(16, 32, $0) },
24-
tags: [.validation, .api]),
25-
24+
runFunction: { with(randomAccess16x32, $0) },
25+
tags: [.validation, .api],
26+
setUpFunction: { blackHole(randomAccess16x32) }),
27+
2628
BenchmarkInfo(
2729
name: "FlattenDistanceFromTo.RandomAccess.32x16",
28-
runFunction: { withRandomAccess(32, 16, $0) },
29-
tags: [.validation, .api]),
30-
30+
runFunction: { with(randomAccess32x16, $0) },
31+
tags: [.validation, .api],
32+
setUpFunction: { blackHole(randomAccess32x16) }),
33+
3134
BenchmarkInfo(
3235
name: "FlattenDistanceFromTo.RandomAccess.32x32",
33-
runFunction: { withRandomAccess(32, 32, $0) },
34-
tags: [.validation, .api]),
36+
runFunction: { with(randomAccess32x32, $0) },
37+
tags: [.validation, .api],
38+
setUpFunction: { blackHole(randomAccess32x32) }),
3539
]
3640

37-
@inline(never)
38-
public func withRandomAccess(
41+
// MARK: - Random Access
42+
43+
func makeRandomAccess(
3944
_ outer: Int,
40-
_ inner: Int,
45+
_ inner: Int
46+
) -> FlattenSequence<Repeated<Repeated<Int>>> {
47+
repeatElement(repeatElement(0, count: inner), count: outer).joined()
48+
}
49+
50+
let randomAccess16x16 = makeRandomAccess(16, 16)
51+
let randomAccess16x32 = makeRandomAccess(16, 32)
52+
let randomAccess32x16 = makeRandomAccess(32, 16)
53+
let randomAccess32x32 = makeRandomAccess(32, 32)
54+
55+
@inline(never)
56+
public func with(
57+
_ collection: FlattenSequence<Repeated<Repeated<Int>>>,
4158
_ iterations: Int
4259
) {
4360
var value = 0 as Int
44-
let minor = repeatElement(00000, count: inner)
45-
let major = repeatElement(minor, count: outer)
46-
let flattened: FlattenSequence = major.joined()
47-
61+
4862
for _ in 0 ..< iterations {
49-
for a in flattened.indices {
50-
for b in flattened.indices {
51-
value &+= flattened.distance(from: a, to: b)
52-
value &+= flattened.distance(from: b, to: a)
63+
for a in collection.indices {
64+
for b in collection.indices {
65+
value &+= collection.distance(from: a, to: b)
66+
value &+= collection.distance(from: b, to: a)
5367
}
5468
}
5569
}
56-
70+
5771
blackHole(value == 0)
5872
}

0 commit comments

Comments
 (0)