Skip to content

Commit 7ed54c9

Browse files
authored
Merge pull request #71786 from oscbyspro/better-joined-distance-from-to-benchmarks
FlattenSequence/distance(from:to:) benchmarks.
2 parents 0c9c734 + 8cfbb62 commit 7ed54c9

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ set(SWIFT_BENCH_MODULES
9595
single-source/ExistentialPerformance
9696
single-source/Fibonacci
9797
single-source/FindStringNaive
98+
single-source/FlattenDistanceFromTo
9899
single-source/FlattenList
99100
single-source/FloatingPointConversion
100101
single-source/FloatingPointParsing
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
//===--- FlattenDistanceFromTo.swift --------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import TestsUtils
14+
15+
public let benchmarks = [
16+
BenchmarkInfo(
17+
name: "FlattenDistanceFromTo.Array.Array.04.04",
18+
runFunction: { with(arrayArray04x04, $0) },
19+
tags: [.validation, .api],
20+
setUpFunction: { blackHole(arrayArray04x04) }),
21+
22+
BenchmarkInfo(
23+
name: "FlattenDistanceFromTo.Array.Array.04x08",
24+
runFunction: { with(arrayArray04x08, $0) },
25+
tags: [.validation, .api],
26+
setUpFunction: { blackHole(arrayArray04x08) }),
27+
28+
BenchmarkInfo(
29+
name: "FlattenDistanceFromTo.Array.Array.08.04",
30+
runFunction: { with(arrayArray08x04, $0) },
31+
tags: [.validation, .api],
32+
setUpFunction: { blackHole(arrayArray08x04) }),
33+
34+
BenchmarkInfo(
35+
name: "FlattenDistanceFromTo.Array.Array.08.08",
36+
runFunction: { with(arrayArray08x08, $0) },
37+
tags: [.validation, .api],
38+
setUpFunction: { blackHole(arrayArray08x08) }),
39+
40+
BenchmarkInfo(
41+
name: "FlattenDistanceFromTo.Array.String.04.04",
42+
runFunction: { with(arrayString04x04, $0) },
43+
tags: [.validation, .api],
44+
setUpFunction: { blackHole(arrayString04x04) }),
45+
46+
BenchmarkInfo(
47+
name: "FlattenDistanceFromTo.Array.String.04.08",
48+
runFunction: { with(arrayString04x08, $0) },
49+
tags: [.validation, .api],
50+
setUpFunction: { blackHole(arrayString04x08) }),
51+
52+
BenchmarkInfo(
53+
name: "FlattenDistanceFromTo.Array.String.08.04",
54+
runFunction: { with(arrayString08x04, $0) },
55+
tags: [.validation, .api],
56+
setUpFunction: { blackHole(arrayString08x04) }),
57+
58+
BenchmarkInfo(
59+
name: "FlattenDistanceFromTo.Array.String.08.08",
60+
runFunction: { with(arrayString08x08, $0) },
61+
tags: [.validation, .api],
62+
setUpFunction: { blackHole(arrayString08x08) }),
63+
]
64+
65+
// MARK: - Array Array
66+
67+
func makeArrayArray(_ outer: Int, _ inner: Int) -> FlattenSequence<[[UInt8]]> {
68+
Array(repeating: Array(repeating: 123, count: inner), count: outer).joined()
69+
}
70+
71+
let arrayArray04x04 = makeArrayArray(04, 04)
72+
let arrayArray04x08 = makeArrayArray(04, 08)
73+
let arrayArray08x04 = makeArrayArray(08, 04)
74+
let arrayArray08x08 = makeArrayArray(08, 08)
75+
76+
@inline(never)
77+
public func with(_ collection: FlattenSequence<[[UInt8]]>, _ iterations: Int) {
78+
var value = 0 as Int
79+
80+
for _ in 0 ..< iterations {
81+
for a in collection.indices {
82+
for b in collection.indices {
83+
value &+= collection.distance(from: a, to: b)
84+
value &+= collection.distance(from: b, to: a)
85+
}
86+
}
87+
}
88+
89+
blackHole(value == 0)
90+
}
91+
92+
// MARK: - Array String
93+
94+
func makeArrayString(_ outer: Int, _ inner: Int) -> FlattenSequence<[String]> {
95+
Array(repeating: String(repeating: "0", count: inner), count: outer).joined()
96+
}
97+
98+
let arrayString04x04 = makeArrayString(04, 04)
99+
let arrayString04x08 = makeArrayString(04, 08)
100+
let arrayString08x04 = makeArrayString(08, 04)
101+
let arrayString08x08 = makeArrayString(08, 08)
102+
103+
@inline(never)
104+
public func with(_ collection: FlattenSequence<[String]>, _ iterations: Int) {
105+
var value = 0 as Int
106+
107+
for _ in 0 ..< iterations {
108+
for a in collection.indices {
109+
for b in collection.indices {
110+
value &+= collection.distance(from: a, to: b)
111+
value &+= collection.distance(from: b, to: a)
112+
}
113+
}
114+
}
115+
116+
blackHole(value == 0)
117+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ import Exclusivity
8989
import ExistentialPerformance
9090
import Fibonacci
9191
import FindStringNaive
92+
import FlattenDistanceFromTo
9293
import FlattenList
9394
import FloatingPointConversion
9495
import FloatingPointParsing
@@ -282,6 +283,7 @@ register(Exclusivity.benchmarks)
282283
register(ExistentialPerformance.benchmarks)
283284
register(Fibonacci.benchmarks)
284285
register(FindStringNaive.benchmarks)
286+
register(FlattenDistanceFromTo.benchmarks)
285287
register(FlattenList.benchmarks)
286288
register(FloatingPointConversion.benchmarks)
287289
register(FloatingPointParsing.benchmarks)

0 commit comments

Comments
 (0)