Skip to content

Commit aa977ad

Browse files
committed
[benchmark] DictionarySwap Setup Overhead
1 parent cdfc31c commit aa977ad

File tree

1 file changed

+45
-106
lines changed

1 file changed

+45
-106
lines changed

benchmark/single-source/DictionarySwap.swift

Lines changed: 45 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -14,67 +14,59 @@
1414
// rdar://problem/19804127
1515
import TestsUtils
1616

17+
let size = 100
18+
let numberMap = Dictionary(uniqueKeysWithValues: zip(1...size, 1...size))
19+
let boxedNums = (1...size).lazy.map { Box($0) }
20+
let boxedNumMap = Dictionary(uniqueKeysWithValues: zip(boxedNums, boxedNums))
21+
1722
let t: [BenchmarkCategory] = [.validation, .api, .Dictionary]
1823

1924
public let DictionarySwap = [
2025
BenchmarkInfo(name: "DictionarySwap",
21-
runFunction: run_DictionarySwap, tags: t, legacyFactor: 4),
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),
2234
BenchmarkInfo(name: "DictionarySwapOfObjects",
23-
runFunction: run_DictionarySwapOfObjects, tags: t, legacyFactor: 40),
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),
2446
BenchmarkInfo(name: "DictionarySwapAt",
25-
runFunction: run_DictionarySwapAt, tags: t, legacyFactor: 4),
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),
2657
BenchmarkInfo(name: "DictionarySwapAtOfObjects",
27-
runFunction: run_DictionarySwapAtOfObjects, tags: t, legacyFactor: 40),
28-
]
29-
30-
@inline(never)
31-
public func run_DictionarySwap(_ N: Int) {
32-
let size = 100
33-
var dict = [Int: Int](minimumCapacity: size)
34-
35-
// Fill dictionary
36-
for i in 1...size {
37-
dict[i] = i
38-
}
39-
CheckResults(dict.count == size)
40-
41-
var swapped = false
42-
for _ in 1...2500*N {
43-
(dict[25], dict[75]) = (dict[75]!, dict[25]!)
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)
4465
swapped = !swapped
45-
if !swappedCorrectly(swapped, dict[25]!, dict[75]!) {
46-
break
47-
}
48-
}
49-
50-
CheckResults(swappedCorrectly(swapped, dict[25]!, dict[75]!))
51-
}
52-
53-
@inline(never)
54-
public func run_DictionarySwapAt(_ N: Int) {
55-
let size = 100
56-
var dict = [Int: Int](minimumCapacity: size)
57-
58-
// Fill dictionary
59-
for i in 1...size {
60-
dict[i] = i
61-
}
62-
CheckResults(dict.count == size)
63-
64-
var swapped = false
65-
for _ in 1...2500*N {
66-
let i25 = dict.index(forKey: 25)!
67-
let i75 = dict.index(forKey: 75)!
68-
69-
dict.values.swapAt(i25, i75)
70-
swapped = !swapped
71-
if !swappedCorrectly(swapped, dict[25]!, dict[75]!) {
72-
break
73-
}
74-
}
75-
76-
CheckResults(swappedCorrectly(swapped, dict[25]!, dict[75]!))
77-
}
66+
CheckResults(swappedCorrectly(swapped,
67+
dict[Box(25)]!.value, dict[Box(75)]!.value))
68+
}}, tags: t, legacyFactor: 40),
69+
]
7870

7971
// Return true if correctly swapped, false otherwise
8072
func swappedCorrectly(_ swapped: Bool, _ p25: Int, _ p75: Int) -> Bool {
@@ -97,56 +89,3 @@ class Box<T : Hashable> : Hashable {
9789
return lhs.value == rhs.value
9890
}
9991
}
100-
101-
@inline(never)
102-
public func run_DictionarySwapOfObjects(_ N: Int) {
103-
let size = 100
104-
var dict = Dictionary<Box<Int>, Box<Int>>(minimumCapacity: size)
105-
106-
// Fill dictionary
107-
for i in 1...size {
108-
dict[Box(i)] = Box(i)
109-
}
110-
CheckResults(dict.count == size)
111-
112-
var swapped = false
113-
for _ in 1...250*N {
114-
let b1 = Box(25)
115-
let b2 = Box(75)
116-
(dict[b1], dict[b2]) = (dict[b2]!, dict[b1]!)
117-
swapped = !swapped
118-
if !swappedCorrectly(swapped, dict[Box(25)]!.value, dict[Box(75)]!.value) {
119-
break
120-
}
121-
}
122-
123-
CheckResults(swappedCorrectly(swapped, dict[Box(25)]!.value, dict[Box(75)]!.value))
124-
}
125-
126-
@inline(never)
127-
public func run_DictionarySwapAtOfObjects(_ N: Int) {
128-
let size = 100
129-
var dict = [Box<Int>: Box<Int>](minimumCapacity: size)
130-
131-
// Fill dictionary
132-
for i in 1...size {
133-
dict[Box(i)] = Box(i)
134-
}
135-
CheckResults(dict.count == size)
136-
137-
var swapped = false
138-
for _ in 1...250*N {
139-
let b25 = Box(25)
140-
let b75 = Box(75)
141-
let i25 = dict.index(forKey: b25)!
142-
let i75 = dict.index(forKey: b75)!
143-
144-
dict.values.swapAt(i25, i75)
145-
swapped = !swapped
146-
if !swappedCorrectly(swapped, dict[Box(25)]!.value, dict[Box(75)]!.value) {
147-
break
148-
}
149-
}
150-
151-
CheckResults(swappedCorrectly(swapped, dict[Box(25)]!.value, dict[Box(75)]!.value))
152-
}

0 commit comments

Comments
 (0)