Skip to content

Commit 09946d0

Browse files
committed
[benchmark] SetTest: Add tests for more operations and more cases
- Isolate legacy tests. Add new tests for the same operations, with updated iteration counts and names. - Rename new tests to follow a consistent naming scheme. - Add tests for Set.subtracting. - Add tests on integer sets with 50% and 100% overlap. (isSubset, intersection, union, symmetricDifference, subtracting)
1 parent 91dc5c6 commit 09946d0

File tree

1 file changed

+198
-54
lines changed

1 file changed

+198
-54
lines changed

benchmark/single-source/SetTests.swift

Lines changed: 198 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,111 +13,231 @@
1313
import TestsUtils
1414

1515
let size = 400
16-
let overlap = 100
16+
let half = size / 2
17+
let quarter = size / 4
1718

19+
// Construction kit for sets with 25% overlap
1820
let setAB = Set(0 ..< size) // 0 ..< 400
1921
let setCD = Set(size ..< 2 * size) // 400 ..< 800
20-
let setBC = Set(size - overlap ..< 2 * size - overlap) // 300 ..< 700
21-
let setB = Set(size - overlap ..< size) // 300 ..< 400
22+
let setBC = Set(size - quarter ..< 2 * size - quarter) // 300 ..< 700
23+
let setB = Set(size - quarter ..< size) // 300 ..< 400
2224

2325
let setOAB = Set(setAB.map(Box.init))
2426
let setOCD = Set(setCD.map(Box.init))
2527
let setOBC = Set(setBC.map(Box.init))
2628
let setOB = Set(setB.map(Box.init))
2729

28-
let countAC = 2 * (size - overlap) // 600
29-
let countABC = 2 * size - overlap // 700
30-
let countABCD = 2 * size // 800
31-
let countB = overlap // 100
30+
let countA = size - quarter // 300
31+
let countB = quarter // 100
32+
let countC = countA // 300
33+
let countD = countB // 100
34+
35+
let countAB = size // 400
36+
let countAC = countA + countC // 600
37+
let countABC = countA + countB + countC // 700
38+
let countABCD = countA + countB + countC + countD // 800
39+
40+
// Construction kit for sets with 50% overlap
41+
let setXY = Set(0 ..< size) // 0 ..< 400
42+
let setYZ = Set(half ..< size + half) // 200 ..< 600
43+
let setY = Set(half ..< size) // 200 ..< 400
44+
45+
// Two sets with 100% overlap, but different bucket counts (let's not make it
46+
// too easy...)
47+
let setP = Set(0 ..< size)
48+
let setQ: Set<Int> = {
49+
var set = Set(0 ..< size)
50+
set.reserveCapacity(2 * size)
51+
return set
52+
}()
3253

3354
public let SetTests = [
55+
// Legacy benchmarks, kept for continuity with previous releases.
3456
BenchmarkInfo(
35-
name: "SetExclusiveOr",
36-
runFunction: { n in run_SetExclusiveOr(setAB, setCD, countABCD, 100 * n) },
57+
name: "SetExclusiveOr", // ~"SetSymmetricDifferenceInt0"
58+
runFunction: { n in run_SetSymmetricDifferenceInt(setAB, setCD, countABCD, 100 * n) },
3759
tags: [.validation, .api, .Set],
3860
setUpFunction: { blackHole([setAB, setCD]) }),
3961
BenchmarkInfo(
40-
name: "SetExclusiveOr2",
41-
runFunction: { n in run_SetExclusiveOr(setAB, setBC, countAC, 10 * n) },
62+
name: "SetExclusiveOr_OfObjects", // ~"SetSymmetricDifferenceBox0"
63+
runFunction: { n in run_SetSymmetricDifferenceBox(setOAB, setOCD, countABCD, 100 * n) },
4264
tags: [.validation, .api, .Set],
43-
setUpFunction: { blackHole([setAB, setBC]) }),
65+
setUpFunction: { blackHole([setOAB, setOCD]) }),
4466
BenchmarkInfo(
45-
name: "SetExclusiveOr_OfObjects",
46-
runFunction: { n in run_SetExclusiveOr_OfObjects(setOAB, setOCD, countABCD, 100 * n) },
67+
name: "SetIntersect", // ~"SetIntersectionInt0"
68+
runFunction: { n in run_SetIntersectionInt(setAB, setCD, 0, 100 * n) },
4769
tags: [.validation, .api, .Set],
48-
setUpFunction: { blackHole([setOAB, setOCD]) }),
70+
setUpFunction: { blackHole([setAB, setCD]) }),
4971
BenchmarkInfo(
50-
name: "SetExclusiveOr2_OfObjects",
51-
runFunction: { n in run_SetExclusiveOr_OfObjects(setOAB, setOBC, countAC, 10 * n) },
72+
name: "SetUnion", // ~"SetUnionInt0"
73+
runFunction: { n in run_SetUnionInt(setAB, setCD, countABCD, 100 * n) },
5274
tags: [.validation, .api, .Set],
53-
setUpFunction: { blackHole([setOAB, setOBC]) }),
75+
setUpFunction: { blackHole([setAB, setCD]) }),
76+
BenchmarkInfo(
77+
name: "SetUnion_OfObjects", // ~"SetUnionBox0"
78+
runFunction: { n in run_SetUnionBox(setOAB, setOCD, countABCD, 100 * n) },
79+
tags: [.validation, .api, .Set],
80+
setUpFunction: { blackHole([setOAB, setOCD]) }),
5481

82+
// Mnemonic: number after name is percentage of common elements in input sets.
5583
BenchmarkInfo(
56-
name: "SetIntersect",
57-
runFunction: { n in run_SetIntersect(setAB, setCD, 0, 100 * n) },
84+
name: "SetIsSubsetInt0",
85+
runFunction: { n in run_SetIsSubsetInt(setAB, setCD, false, 5000 * n) },
5886
tags: [.validation, .api, .Set],
5987
setUpFunction: { blackHole([setAB, setCD]) }),
6088
BenchmarkInfo(
61-
name: "SetIntersect2",
62-
runFunction: { n in run_SetIntersect(setAB, setBC, countB, 10 * n) },
89+
name: "SetIsSubsetBox0",
90+
runFunction: { n in run_SetIsSubsetBox(setOAB, setOCD, false, 5000 * n) },
6391
tags: [.validation, .api, .Set],
64-
setUpFunction: { blackHole([setAB, setBC]) }),
92+
setUpFunction: { blackHole([setOAB, setOCD]) }),
93+
BenchmarkInfo(
94+
name: "SetIsSubsetInt25",
95+
runFunction: { n in run_SetIsSubsetInt(setB, setAB, true, 50 * n) },
96+
tags: [.validation, .api, .Set],
97+
setUpFunction: { blackHole([setB, setAB]) }),
98+
BenchmarkInfo(
99+
name: "SetIsSubsetBox25",
100+
runFunction: { n in run_SetIsSubsetBox(setOB, setOAB, true, 50 * n) },
101+
tags: [.validation, .api, .Set],
102+
setUpFunction: { blackHole([setOB, setOAB]) }),
103+
BenchmarkInfo(
104+
name: "SetIsSubsetInt50",
105+
runFunction: { n in run_SetIsSubsetInt(setY, setXY, true, 50 * n) },
106+
tags: [.validation, .api, .Set],
107+
setUpFunction: { blackHole([setY, setXY]) }),
65108
BenchmarkInfo(
66-
name: "SetIntersect_OfObjects",
67-
runFunction: { n in run_SetIntersect_OfObjects(setOAB, setOCD, 0, 100 * n) },
109+
name: "SetIsSubsetInt100",
110+
runFunction: { n in run_SetIsSubsetInt(setP, setQ, true, 50 * n) },
111+
tags: [.validation, .api, .Set],
112+
setUpFunction: { blackHole([setP, setQ]) }),
113+
114+
BenchmarkInfo(
115+
name: "SetSymmetricDifferenceInt0",
116+
runFunction: { n in run_SetSymmetricDifferenceInt(setAB, setCD, countABCD, 10 * n) },
117+
tags: [.validation, .api, .Set],
118+
setUpFunction: { blackHole([setAB, setCD]) }),
119+
BenchmarkInfo(
120+
name: "SetSymmetricDifferenceBox0",
121+
runFunction: { n in run_SetSymmetricDifferenceBox(setOAB, setOCD, countABCD, 10 * n) },
68122
tags: [.validation, .api, .Set],
69123
setUpFunction: { blackHole([setOAB, setOCD]) }),
70124
BenchmarkInfo(
71-
name: "SetIntersect2_OfObjects",
72-
runFunction: { n in run_SetIntersect_OfObjects(setOAB, setOBC, countB, 10 * n) },
125+
name: "SetSymmetricDifferenceInt25",
126+
runFunction: { n in run_SetSymmetricDifferenceInt(setAB, setBC, countAC, 10 * n) },
127+
tags: [.validation, .api, .Set],
128+
setUpFunction: { blackHole([setAB, setBC]) }),
129+
BenchmarkInfo(
130+
name: "SetSymmetricDifferenceBox25",
131+
runFunction: { n in run_SetSymmetricDifferenceBox(setOAB, setOBC, countAC, 10 * n) },
73132
tags: [.validation, .api, .Set],
74133
setUpFunction: { blackHole([setOAB, setOBC]) }),
134+
BenchmarkInfo(
135+
name: "SetSymmetricDifferenceInt50",
136+
runFunction: { n in run_SetSymmetricDifferenceInt(setXY, setYZ, size, 10 * n) },
137+
tags: [.validation, .api, .Set],
138+
setUpFunction: { blackHole([setXY, setYZ]) }),
139+
BenchmarkInfo(
140+
name: "SetSymmetricDifferenceInt100",
141+
runFunction: { n in run_SetSymmetricDifferenceInt(setP, setQ, 0, 10 * n) },
142+
tags: [.validation, .api, .Set],
143+
setUpFunction: { blackHole([setP, setQ]) }),
75144

76145
BenchmarkInfo(
77-
name: "SetIsSubsetOf",
78-
runFunction: { n in run_SetIsSubsetOf(setAB, setCD, false, 5000 * n) },
146+
name: "SetIntersectionInt0",
147+
runFunction: { n in run_SetIntersectionInt(setAB, setCD, 0, 10 * n) },
79148
tags: [.validation, .api, .Set],
80149
setUpFunction: { blackHole([setAB, setCD]) }),
81150
BenchmarkInfo(
82-
name: "SetIsSubsetOf2",
83-
runFunction: { n in run_SetIsSubsetOf(setB, setAB, true, 50 * n) },
151+
name: "SetIntersectionBox0",
152+
runFunction: { n in run_SetIntersectionBox(setOAB, setOCD, 0, 10 * n) },
84153
tags: [.validation, .api, .Set],
85-
setUpFunction: { blackHole([setB, setAB]) }),
154+
setUpFunction: { blackHole([setOAB, setOCD]) }),
86155
BenchmarkInfo(
87-
name: "SetIsSubsetOf_OfObjects",
88-
runFunction: { n in run_SetIsSubsetOf_OfObjects(setOAB, setOCD, false, 5000 * n) },
156+
name: "SetIntersectionInt25",
157+
runFunction: { n in run_SetIntersectionInt(setAB, setBC, countB, 10 * n) },
89158
tags: [.validation, .api, .Set],
90-
setUpFunction: { blackHole([setOAB, setOCD]) }),
159+
setUpFunction: { blackHole([setAB, setBC]) }),
91160
BenchmarkInfo(
92-
name: "SetIsSubsetOf2_OfObjects",
93-
runFunction: { n in run_SetIsSubsetOf_OfObjects(setOB, setOAB, true, 50 * n) },
161+
name: "SetIntersectionBox25",
162+
runFunction: { n in run_SetIntersectionBox(setOAB, setOBC, countB, 10 * n) },
94163
tags: [.validation, .api, .Set],
95-
setUpFunction: { blackHole([setOB, setOAB]) }),
164+
setUpFunction: { blackHole([setOAB, setOBC]) }),
165+
BenchmarkInfo(
166+
name: "SetIntersectionInt50",
167+
runFunction: { n in run_SetIntersectionInt(setXY, setYZ, half, 10 * n) },
168+
tags: [.validation, .api, .Set],
169+
setUpFunction: { blackHole([setXY, setYZ]) }),
170+
BenchmarkInfo(
171+
name: "SetIntersectionInt100",
172+
runFunction: { n in run_SetIntersectionInt(setP, setQ, size, 10 * n) },
173+
tags: [.validation, .api, .Set],
174+
setUpFunction: { blackHole([setP, setQ]) }),
96175

97176
BenchmarkInfo(
98-
name: "SetUnion",
99-
runFunction: { n in run_SetUnion(setAB, setCD, countABCD, 100 * n) },
177+
name: "SetUnionInt0",
178+
runFunction: { n in run_SetUnionInt(setAB, setCD, countABCD, 10 * n) },
100179
tags: [.validation, .api, .Set],
101180
setUpFunction: { blackHole([setAB, setCD]) }),
102181
BenchmarkInfo(
103-
name: "SetUnion2",
104-
runFunction: { n in run_SetUnion(setAB, setBC, countABC, 10 * n) },
182+
name: "SetUnionBox0",
183+
runFunction: { n in run_SetUnionBox(setOAB, setOCD, countABCD, 10 * n) },
184+
tags: [.validation, .api, .Set],
185+
setUpFunction: { blackHole([setOAB, setOCD]) }),
186+
BenchmarkInfo(
187+
name: "SetUnionInt25",
188+
runFunction: { n in run_SetUnionInt(setAB, setBC, countABC, 10 * n) },
105189
tags: [.validation, .api, .Set],
106190
setUpFunction: { blackHole([setAB, setBC]) }),
107191
BenchmarkInfo(
108-
name: "SetUnion_OfObjects",
109-
runFunction: { n in run_SetUnion_OfObjects(setOAB, setOCD, countABCD, 100 * n) },
192+
name: "SetUnionBox25",
193+
runFunction: { n in run_SetUnionBox(setOAB, setOBC, countABC, 10 * n) },
194+
tags: [.validation, .api, .Set],
195+
setUpFunction: { blackHole([setOAB, setOBC]) }),
196+
BenchmarkInfo(
197+
name: "SetUnionInt50",
198+
runFunction: { n in run_SetUnionInt(setXY, setYZ, size + half, 10 * n) },
199+
tags: [.validation, .api, .Set],
200+
setUpFunction: { blackHole([setXY, setYZ]) }),
201+
BenchmarkInfo(
202+
name: "SetUnionInt100",
203+
runFunction: { n in run_SetUnionInt(setP, setQ, size, 10 * n) },
204+
tags: [.validation, .api, .Set],
205+
setUpFunction: { blackHole([setP, setQ]) }),
206+
207+
BenchmarkInfo(
208+
name: "SetSubtractingInt0",
209+
runFunction: { n in run_SetSubtractingInt(setAB, setCD, countAB, 10 * n) },
210+
tags: [.validation, .api, .Set],
211+
setUpFunction: { blackHole([setAB, setCD]) }),
212+
BenchmarkInfo(
213+
name: "SetSubtractingBox0",
214+
runFunction: { n in run_SetSubtractingBox(setOAB, setOCD, countAB, 10 * n) },
110215
tags: [.validation, .api, .Set],
111216
setUpFunction: { blackHole([setOAB, setOCD]) }),
112217
BenchmarkInfo(
113-
name: "SetUnion2_OfObjects",
114-
runFunction: { n in run_SetUnion_OfObjects(setOAB, setOBC, countABC, 10 * n) },
218+
name: "SetSubtractingInt25",
219+
runFunction: { n in run_SetSubtractingInt(setAB, setBC, countA, 10 * n) },
220+
tags: [.validation, .api, .Set],
221+
setUpFunction: { blackHole([setAB, setBC]) }),
222+
BenchmarkInfo(
223+
name: "SetSubtractingBox25",
224+
runFunction: { n in run_SetSubtractingBox(setOAB, setOBC, countA, 10 * n) },
115225
tags: [.validation, .api, .Set],
116226
setUpFunction: { blackHole([setOAB, setOBC]) }),
227+
BenchmarkInfo(
228+
name: "SetSubtractingInt50",
229+
runFunction: { n in run_SetSubtractingInt(setXY, setYZ, half, 10 * n) },
230+
tags: [.validation, .api, .Set],
231+
setUpFunction: { blackHole([setXY, setYZ]) }),
232+
BenchmarkInfo(
233+
name: "SetSubtractingInt100",
234+
runFunction: { n in run_SetSubtractingInt(setP, setQ, 0, 10 * n) },
235+
tags: [.validation, .api, .Set],
236+
setUpFunction: { blackHole([setP, setQ]) }),
117237
]
118238

119239
@inline(never)
120-
public func run_SetIsSubsetOf(
240+
public func run_SetIsSubsetInt(
121241
_ a: Set<Int>,
122242
_ b: Set<Int>,
123243
_ r: Bool,
@@ -129,7 +249,7 @@ public func run_SetIsSubsetOf(
129249
}
130250

131251
@inline(never)
132-
public func run_SetExclusiveOr(
252+
public func run_SetSymmetricDifferenceInt(
133253
_ a: Set<Int>,
134254
_ b: Set<Int>,
135255
_ r: Int,
@@ -141,7 +261,7 @@ public func run_SetExclusiveOr(
141261
}
142262

143263
@inline(never)
144-
public func run_SetUnion(
264+
public func run_SetUnionInt(
145265
_ a: Set<Int>,
146266
_ b: Set<Int>,
147267
_ r: Int,
@@ -153,7 +273,7 @@ public func run_SetUnion(
153273
}
154274

155275
@inline(never)
156-
public func run_SetIntersect(
276+
public func run_SetIntersectionInt(
157277
_ a: Set<Int>,
158278
_ b: Set<Int>,
159279
_ r: Int,
@@ -164,6 +284,18 @@ public func run_SetIntersect(
164284
}
165285
}
166286

287+
@inline(never)
288+
public func run_SetSubtractingInt(
289+
_ a: Set<Int>,
290+
_ b: Set<Int>,
291+
_ r: Int,
292+
_ n: Int) {
293+
for _ in 0 ..< n {
294+
let and = a.subtracting(identity(b))
295+
CheckResults(and.count == r)
296+
}
297+
}
298+
167299
class Box<T : Hashable> : Hashable {
168300
var value: T
169301

@@ -181,7 +313,7 @@ class Box<T : Hashable> : Hashable {
181313
}
182314

183315
@inline(never)
184-
func run_SetIsSubsetOf_OfObjects(
316+
func run_SetIsSubsetBox(
185317
_ a: Set<Box<Int>>,
186318
_ b: Set<Box<Int>>,
187319
_ r: Bool,
@@ -193,7 +325,7 @@ func run_SetIsSubsetOf_OfObjects(
193325
}
194326

195327
@inline(never)
196-
func run_SetExclusiveOr_OfObjects(
328+
func run_SetSymmetricDifferenceBox(
197329
_ a: Set<Box<Int>>,
198330
_ b: Set<Box<Int>>,
199331
_ r: Int,
@@ -205,7 +337,7 @@ func run_SetExclusiveOr_OfObjects(
205337
}
206338

207339
@inline(never)
208-
func run_SetUnion_OfObjects(
340+
func run_SetUnionBox(
209341
_ a: Set<Box<Int>>,
210342
_ b: Set<Box<Int>>,
211343
_ r: Int,
@@ -217,7 +349,7 @@ func run_SetUnion_OfObjects(
217349
}
218350

219351
@inline(never)
220-
func run_SetIntersect_OfObjects(
352+
func run_SetIntersectionBox(
221353
_ a: Set<Box<Int>>,
222354
_ b: Set<Box<Int>>,
223355
_ r: Int,
@@ -227,3 +359,15 @@ func run_SetIntersect_OfObjects(
227359
CheckResults(and.count == r)
228360
}
229361
}
362+
363+
@inline(never)
364+
func run_SetSubtractingBox(
365+
_ a: Set<Box<Int>>,
366+
_ b: Set<Box<Int>>,
367+
_ r: Int,
368+
_ n: Int) {
369+
for _ in 0 ..< n {
370+
let and = a.subtracting(b)
371+
CheckResults(and.count == r)
372+
}
373+
}

0 commit comments

Comments
 (0)