Skip to content

Commit 06a2e6d

Browse files
[test] Add Range overlaps benchmark
1 parent 875cd65 commit 06a2e6d

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-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: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
BenchmarkInfo(
21+
name: "RangeOverlapsClosedRange",
22+
runFunction: run_RangeOverlapsClosedRange,
23+
tags: [.validation, .api]),
24+
BenchmarkInfo(
25+
name: "ClosedRangeOverlapsClosedRange",
26+
runFunction: run_ClosedRangeOverlapsClosedRange,
27+
tags: [.validation, .api]),
28+
]
29+
30+
@inline(never) func foo() { }
31+
32+
@inline(never)
33+
public func run_RangeOverlapsRange(_ N: Int) {
34+
var ranges: [Range<Int>] = []
35+
for a in -20 ... 20 {
36+
for b in 0 ... 40 {
37+
ranges.append(a ..< (a+b))
38+
}
39+
}
40+
var check: UInt64 = 0
41+
for _ in 0..<N {
42+
for lhs in ranges {
43+
for rhs in ranges {
44+
if lhs.overlaps(rhs) { check += 1 }
45+
}
46+
}
47+
}
48+
CheckResults(check == 1771200 * UInt64(N))
49+
}
50+
51+
@inline(never)
52+
public func run_RangeOverlapsClosedRange(_ N: Int) {
53+
var ranges: [Range<Int>] = []
54+
var closedRanges: [ClosedRange<Int>] = []
55+
for a in -20 ... 20 {
56+
for b in 0 ... 40 {
57+
ranges.append(a ..< (a+b))
58+
closedRanges.append(a ... (a+b))
59+
}
60+
}
61+
var check: UInt64 = 0
62+
for _ in 0..<N {
63+
for lhs in ranges {
64+
for rhs in closedRanges {
65+
if lhs.overlaps(rhs) { check += 1 }
66+
}
67+
}
68+
}
69+
CheckResults(check == 1826960 * UInt64(N))
70+
}
71+
72+
@inline(never)
73+
public func run_ClosedRangeOverlapsClosedRange(_ N: Int) {
74+
var closedRanges: [ClosedRange<Int>] = []
75+
for a in -20 ... 20 {
76+
for b in 0 ... 40 {
77+
closedRanges.append(a ... (a+b))
78+
}
79+
}
80+
var check: UInt64 = 0
81+
for _ in 0..<N {
82+
for lhs in closedRanges {
83+
for rhs in closedRanges {
84+
if lhs.overlaps(rhs) { check += 1 }
85+
}
86+
}
87+
}
88+
CheckResults(check == 1884401 * UInt64(N))
89+
}

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)