Skip to content

Commit 6bfaafa

Browse files
committed
[benchmark] DictionarySwap: Extract runFunctions
1 parent a123b22 commit 6bfaafa

File tree

1 file changed

+51
-40
lines changed

1 file changed

+51
-40
lines changed

benchmark/single-source/DictionarySwap.swift

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,49 +23,13 @@ let t: [BenchmarkCategory] = [.validation, .api, .Dictionary]
2323

2424
public let DictionarySwap = [
2525
BenchmarkInfo(name: "DictionarySwap",
26-
runFunction: {
27-
var dict = numberMap
28-
var swapped = false
29-
for _ in 1...$0*2500 {
30-
(dict[25], dict[75]) = (dict[75]!, dict[25]!)
31-
swapped = !swapped
32-
CheckResults(swappedCorrectly(swapped, dict[25]!, dict[75]!))
33-
}}, tags: t, legacyFactor: 4),
26+
runFunction: swap, tags: t, legacyFactor: 4),
3427
BenchmarkInfo(name: "DictionarySwapOfObjects",
35-
runFunction: {
36-
var dict = boxedNumMap
37-
var swapped = false
38-
for _ in 1...$0*250 {
39-
let b1 = Box(25)
40-
let b2 = Box(75)
41-
(dict[b1], dict[b2]) = (dict[b2]!, dict[b1]!)
42-
swapped = !swapped
43-
CheckResults(swappedCorrectly(swapped,
44-
dict[Box(25)]!.value, dict[Box(75)]!.value))
45-
}}, tags: t, legacyFactor: 40),
28+
runFunction: swapObjects, tags: t, legacyFactor: 40),
4629
BenchmarkInfo(name: "DictionarySwapAt",
47-
runFunction: {
48-
var dict = numberMap
49-
var swapped = false
50-
for _ in 1...$0*2500 {
51-
let i25 = dict.index(forKey: 25)!
52-
let i75 = dict.index(forKey: 75)!
53-
dict.values.swapAt(i25, i75)
54-
swapped = !swapped
55-
CheckResults(swappedCorrectly(swapped, dict[25]!, dict[75]!))
56-
}}, tags: t, legacyFactor: 4),
30+
runFunction: swapAt, tags: t, legacyFactor: 4),
5731
BenchmarkInfo(name: "DictionarySwapAtOfObjects",
58-
runFunction: {
59-
var dict = boxedNumMap
60-
var swapped = false
61-
for _ in 1...$0*250 {
62-
let i25 = dict.index(forKey: Box(25))!
63-
let i75 = dict.index(forKey: Box(75))!
64-
dict.values.swapAt(i25, i75)
65-
swapped = !swapped
66-
CheckResults(swappedCorrectly(swapped,
67-
dict[Box(25)]!.value, dict[Box(75)]!.value))
68-
}}, tags: t, legacyFactor: 40),
32+
runFunction: swapAtObjects, tags: t, legacyFactor: 40),
6933
]
7034

7135
// Return true if correctly swapped, false otherwise
@@ -89,3 +53,50 @@ class Box<T : Hashable> : Hashable {
8953
return lhs.value == rhs.value
9054
}
9155
}
56+
57+
func swap(N: Int) {
58+
var dict = numberMap
59+
var swapped = false
60+
for _ in 1...2500*N {
61+
(dict[25], dict[75]) = (dict[75]!, dict[25]!)
62+
swapped = !swapped
63+
CheckResults(swappedCorrectly(swapped, dict[25]!, dict[75]!))
64+
}
65+
}
66+
67+
func swapObjects(N: Int) {
68+
var dict = boxedNumMap
69+
var swapped = false
70+
for _ in 1...250*N {
71+
let b1 = Box(25)
72+
let b2 = Box(75)
73+
(dict[b1], dict[b2]) = (dict[b2]!, dict[b1]!)
74+
swapped = !swapped
75+
CheckResults(swappedCorrectly(swapped,
76+
dict[Box(25)]!.value, dict[Box(75)]!.value))
77+
}
78+
}
79+
80+
func swapAt(N: Int) {
81+
var dict = numberMap
82+
var swapped = false
83+
for _ in 1...2500*N {
84+
let i25 = dict.index(forKey: 25)!
85+
let i75 = dict.index(forKey: 75)!
86+
dict.values.swapAt(i25, i75)
87+
swapped = !swapped
88+
CheckResults(swappedCorrectly(swapped, dict[25]!, dict[75]!))
89+
}
90+
}
91+
92+
func swapAtObjects(N: Int) {
93+
var dict = boxedNumMap
94+
var swapped = false
95+
for _ in 1...250*N {
96+
let i25 = dict.index(forKey: Box(25))!
97+
let i75 = dict.index(forKey: Box(75))!
98+
dict.values.swapAt(i25, i75)
99+
swapped = !swapped
100+
CheckResults(swappedCorrectly(swapped,
101+
dict[Box(25)]!.value, dict[Box(75)]!.value))
102+
}}

0 commit comments

Comments
 (0)