Skip to content

Commit 0ea8985

Browse files
committed
FlattenSequence/distance(from:to:) benchmarks (swiftlang#71648).
1 parent 23a0186 commit 0ea8985

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 FlattenDistanceFromTo = [
16+
BenchmarkInfo(
17+
name: "FlattenDistanceFromTo.RandomAccess.16x16",
18+
runFunction: { withRandomAccess(16, 16, $0) },
19+
tags: [.validation, .api]),
20+
21+
BenchmarkInfo(
22+
name: "FlattenDistanceFromTo.RandomAccess.16x32",
23+
runFunction: { withRandomAccess(16, 32, $0) },
24+
tags: [.validation, .api]),
25+
26+
BenchmarkInfo(
27+
name: "FlattenDistanceFromTo.RandomAccess.32x16",
28+
runFunction: { withRandomAccess(32, 16, $0) },
29+
tags: [.validation, .api]),
30+
31+
BenchmarkInfo(
32+
name: "FlattenDistanceFromTo.RandomAccess.32x32",
33+
runFunction: { withRandomAccess(32, 32, $0) },
34+
tags: [.validation, .api]),
35+
]
36+
37+
@inline(never)
38+
public func withRandomAccess(
39+
_ outer: Int,
40+
_ inner: Int,
41+
_ iterations: Int
42+
) {
43+
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+
48+
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)
53+
}
54+
}
55+
}
56+
57+
blackHole(value == 0)
58+
}

benchmark/utils/main.swift

Lines changed: 1 addition & 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

0 commit comments

Comments
 (0)