Skip to content

Commit 9250e9f

Browse files
Merge pull request #23294 from PatrickPijnappel/range-overlaps-performance
[test] Add Range Overlaps Benchmark
2 parents 5ca9ddc + 143f253 commit 9250e9f

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ set(SWIFT_BENCH_MODULES
134134
single-source/RandomValues
135135
single-source/RangeAssignment
136136
single-source/RangeIteration
137+
single-source/RangeOverlaps
137138
single-source/RangeReplaceableCollectionPlusDefault
138139
single-source/RecursiveOwnedParameter
139140
single-source/ReduceInto
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//===--- RangeOverlaps.swift ----------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 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 RangeOverlaps = [
16+
BenchmarkInfo(
17+
name: "RangeOverlapsRange",
18+
runFunction: run_RangeOverlapsRange,
19+
tags: [.validation, .api],
20+
setUpFunction: buildRanges),
21+
BenchmarkInfo(
22+
name: "RangeOverlapsClosedRange",
23+
runFunction: run_RangeOverlapsClosedRange,
24+
tags: [.validation, .api],
25+
setUpFunction: buildRanges),
26+
BenchmarkInfo(
27+
name: "ClosedRangeOverlapsClosedRange",
28+
runFunction: run_ClosedRangeOverlapsClosedRange,
29+
tags: [.validation, .api],
30+
setUpFunction: buildRanges)
31+
]
32+
33+
private func buildRanges() {
34+
blackHole(ranges)
35+
blackHole(closedRanges)
36+
}
37+
38+
private let ranges: [Range<Int>] = (-8...8).flatMap { a in (0...16).map { l in a..<(a+l) } }
39+
private let closedRanges: [ClosedRange<Int>] = (-8...8).flatMap { a in (0...16).map { l in a...(a+l) } }
40+
41+
@inline(never)
42+
public func run_RangeOverlapsRange(_ N: Int) {
43+
var check: UInt64 = 0
44+
for _ in 0..<N {
45+
for lhs in ranges {
46+
for rhs in ranges {
47+
if lhs.overlaps(rhs) { check += 1 }
48+
}
49+
}
50+
}
51+
CheckResults(check == 47872 * UInt64(N))
52+
}
53+
54+
@inline(never)
55+
public func run_RangeOverlapsClosedRange(_ N: Int) {
56+
var check: UInt64 = 0
57+
for _ in 0..<N {
58+
for lhs in ranges {
59+
for rhs in closedRanges {
60+
if lhs.overlaps(rhs) { check += 1 }
61+
}
62+
}
63+
}
64+
CheckResults(check == 51680 * UInt64(N))
65+
}
66+
67+
@inline(never)
68+
public func run_ClosedRangeOverlapsClosedRange(_ N: Int) {
69+
var check: UInt64 = 0
70+
for _ in 0..<N {
71+
for lhs in closedRanges {
72+
for rhs in closedRanges {
73+
if lhs.overlaps(rhs) { check += 1 }
74+
}
75+
}
76+
}
77+
CheckResults(check == 55777 * UInt64(N))
78+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ import RandomShuffle
127127
import RandomValues
128128
import RangeAssignment
129129
import RangeIteration
130+
import RangeOverlaps
130131
import RangeReplaceableCollectionPlusDefault
131132
import RecursiveOwnedParameter
132133
import ReduceInto
@@ -298,6 +299,7 @@ registerBenchmark(RandomShuffle)
298299
registerBenchmark(RandomValues)
299300
registerBenchmark(RangeAssignment)
300301
registerBenchmark(RangeIteration)
302+
registerBenchmark(RangeOverlaps)
301303
registerBenchmark(RangeReplaceableCollectionPlusDefault)
302304
registerBenchmark(RecursiveOwnedParameter)
303305
registerBenchmark(ReduceInto)

0 commit comments

Comments
 (0)