Skip to content

Commit 2eb2278

Browse files
authored
Add benchmarks to measure Dictionary.filter (#59264)
* Add benchmarks for dictionary filtering
1 parent 48ee0a6 commit 2eb2278

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

benchmark/single-source/DictionaryRemove.swift

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,32 @@ let boxedNums = (1...size).lazy.map { Box($0) }
2222
let boxedNumMap = Dictionary(uniqueKeysWithValues: zip(boxedNums, boxedNums))
2323

2424
public let benchmarks = [
25-
BenchmarkInfo(name: "DictionaryRemove",
26-
runFunction: remove, tags: t, legacyFactor: 10),
27-
BenchmarkInfo(name: "DictionaryRemoveOfObjects",
28-
runFunction: removeObjects, tags: t, legacyFactor: 100),
25+
BenchmarkInfo(
26+
name: "DictionaryRemove",
27+
runFunction: remove,
28+
tags: t,
29+
setUpFunction: { blackHole(numberMap) },
30+
legacyFactor: 10),
31+
BenchmarkInfo(
32+
name: "DictionaryRemoveOfObjects",
33+
runFunction: removeObjects,
34+
tags: t,
35+
setUpFunction: { blackHole(boxedNumMap) },
36+
legacyFactor: 100),
37+
38+
BenchmarkInfo(
39+
name: "DictionaryFilter",
40+
runFunction: filter,
41+
tags: t,
42+
setUpFunction: { blackHole(numberMap) },
43+
legacyFactor: 1),
44+
45+
BenchmarkInfo(
46+
name: "DictionaryFilterOfObjects",
47+
runFunction: filterObjects,
48+
tags: t,
49+
setUpFunction: { blackHole(boxedNumMap) },
50+
legacyFactor: 1),
2951
]
3052

3153
class Box<T : Hashable> : Hashable {
@@ -59,3 +81,19 @@ func removeObjects(n: Int) {
5981
check(dict.isEmpty)
6082
}
6183
}
84+
85+
func filter(n: Int) {
86+
for _ in 1...1000*n {
87+
let dict = numberMap
88+
let result = dict.filter {key, value in value % 2 == 0}
89+
check(result.count == size/2)
90+
}
91+
}
92+
93+
func filterObjects(n: Int) {
94+
for _ in 1...1000*n {
95+
let dict = boxedNumMap
96+
let result = dict.filter {key, value in value.value % 2 == 0}
97+
check(result.count == size/2)
98+
}
99+
}

0 commit comments

Comments
 (0)