Skip to content

Commit 8e67642

Browse files
committed
[benchmark] Dict.[CopyKeyValue, FilterAllMatch]
Split the composite tests from `DictionatyCopy` and `DictionaryFilter` into individual benchmarks by dictionary size. Lowered the workloads to run faster (more stable results).
1 parent 16af31a commit 8e67642

File tree

1 file changed

+38
-50
lines changed

1 file changed

+38
-50
lines changed

benchmark/single-source/DictionaryCopy.swift

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,66 +17,54 @@
1717

1818
import TestsUtils
1919

20-
public let DictionaryCopy = [
21-
BenchmarkInfo(name: "DictionaryCopy", runFunction: run_DictionaryCopy,
22-
tags: [.validation, .api, .Dictionary]),
23-
BenchmarkInfo(name: "DictionaryFilter", runFunction: run_DictionaryFilter,
24-
tags: [.validation, .api, .Dictionary]),
25-
]
20+
let t: [BenchmarkCategory] = [.validation, .api, .Dictionary]
2621

27-
@inline(never)
28-
public func testCopy(_ size: Int) {
29-
var dict1 = [Int: Int](minimumCapacity: size)
22+
// We run the test at a spread of sizes between 1*x and 2*x, because the
23+
// quadratic behavior only happens at certain load factors.
3024

31-
// Fill dictionary
32-
for i in 1...size {
33-
dict1[i] = 2 * i
34-
}
35-
CheckResults(dict1.count == size)
25+
public let DictionaryCopy = [
26+
BenchmarkInfo(name:"Dict.CopyKeyValue.16k",
27+
runFunction: copyKeyValue, tags: t, setUpFunction: { dict(16_000) }),
28+
BenchmarkInfo(name:"Dict.CopyKeyValue.20k",
29+
runFunction: copyKeyValue, tags: t, setUpFunction: { dict(20_000) }),
30+
BenchmarkInfo(name:"Dict.CopyKeyValue.24k",
31+
runFunction: copyKeyValue, tags: t, setUpFunction: { dict(24_000) }),
32+
BenchmarkInfo(name:"Dict.CopyKeyValue.28k",
33+
runFunction: copyKeyValue, tags: t, setUpFunction: { dict(28_000) }),
3634

37-
var dict2 = [Int: Int]()
38-
for (key, value) in dict1 {
39-
dict2[key] = value
40-
}
41-
CheckResults(dict2.count == size)
42-
}
35+
BenchmarkInfo(name:"Dict.FilterAllMatch.16k",
36+
runFunction: filterAllMatch, tags: t, setUpFunction: { dict(16_000) }),
37+
BenchmarkInfo(name:"Dict.FilterAllMatch.20k",
38+
runFunction: filterAllMatch, tags: t, setUpFunction: { dict(20_000) }),
39+
BenchmarkInfo(name:"Dict.FilterAllMatch.24k",
40+
runFunction: filterAllMatch, tags: t, setUpFunction: { dict(24_000) }),
41+
BenchmarkInfo(name:"Dict.FilterAllMatch.28k",
42+
runFunction: filterAllMatch, tags: t, setUpFunction: { dict(28_000) }),
43+
]
4344

44-
@inline(never)
45-
public func run_DictionaryCopy(_ N: Int) {
46-
for _ in 0 ..< N {
47-
// We run the test at a spread of sizes between 1*x and 2*x, because the
48-
// quadratic behavior only happens at certain load factors.
49-
testCopy(100_000)
50-
testCopy(125_000)
51-
testCopy(150_000)
52-
testCopy(175_000)
53-
}
45+
var dict: [Int: Int]?
46+
47+
func dict(_ size: Int) {
48+
dict = Dictionary(uniqueKeysWithValues: zip(1...size, 1...size))
5449
}
5550

5651
@inline(never)
57-
public func testFilter(_ size: Int) {
58-
var dict1 = [Int: Int](minimumCapacity: size)
59-
60-
// Fill dictionary
61-
for i in 1...size {
62-
dict1[i] = 2 * i
52+
func copyKeyValue(N: Int) {
53+
for _ in 1...N {
54+
var copy = [Int: Int]()
55+
for (key, value) in dict! {
56+
copy[key] = value
57+
}
58+
CheckResults(copy.count == dict!.count)
6359
}
64-
CheckResults(dict1.count == size)
65-
66-
// Filter with a predicate returning true is essentially the same loop as the
67-
// one in testCopy above.
68-
let dict2 = dict1.filter { _ in true }
69-
CheckResults(dict2.count == size)
7060
}
7161

62+
// Filter with a predicate returning true is essentially the same loop as the
63+
// one in copyKeyValue above.
7264
@inline(never)
73-
public func run_DictionaryFilter(_ N: Int) {
74-
for _ in 0 ..< N {
75-
// We run the test at a spread of sizes between 1*x and 2*x, because the
76-
// quadratic behavior only happens at certain load factors.
77-
testFilter(100_000)
78-
testFilter(125_000)
79-
testFilter(150_000)
80-
testFilter(175_000)
65+
func filterAllMatch(N: Int) {
66+
for _ in 1...N {
67+
let copy = dict!.filter { _ in true }
68+
CheckResults(copy.count == dict!.count)
8169
}
8270
}

0 commit comments

Comments
 (0)