Skip to content

Commit 4e43f74

Browse files
committed
[benchmark] Add DictionaryRemoveOfObjects
Another dictionary benchmark with objects as key and value types.
1 parent f6a1926 commit 4e43f74

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

benchmark/single-source/DictionaryRemove.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,50 @@ public func run_DictionaryRemove(N: Int) {
4141
CheckResults(tmpDict.isEmpty,
4242
"tmpDict should be empty: \(tmpDict.count) != 0.")
4343
}
44+
45+
class Box<T : Hashable where T : Equatable> : Hashable {
46+
var value: T
47+
48+
init(_ v: T) {
49+
value = v
50+
}
51+
52+
var hashValue : Int {
53+
return value.hashValue
54+
}
55+
}
56+
57+
extension Box : Equatable {
58+
}
59+
60+
func ==<T: Equatable>(lhs: Box<T>, rhs: Box<T>) -> Bool {
61+
return lhs.value == rhs.value
62+
}
63+
64+
@inline(never)
65+
public func run_DictionaryRemoveOfObjects(N: Int) {
66+
let size = 100
67+
var dict = Dictionary<Box<Int>, Box<Int>>(minimumCapacity: size)
68+
69+
// Fill dictionary
70+
for i in 1...size {
71+
dict[Box(i)] = Box(i)
72+
}
73+
CheckResults(dict.count == size,
74+
"Incorrect dict count: \(dict.count) != \(size).")
75+
76+
var tmpDict = dict
77+
for _ in 1...1000*N {
78+
tmpDict = dict
79+
// Empty dictionary
80+
for i in 1...size {
81+
tmpDict.removeValue(forKey: Box(i))
82+
}
83+
if !tmpDict.isEmpty {
84+
break
85+
}
86+
}
87+
88+
CheckResults(tmpDict.isEmpty,
89+
"tmpDict should be empty: \(tmpDict.count) != 0.")
90+
}

benchmark/utils/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ precommitTests = [
127127
"DictionaryBridge": run_DictionaryBridge,
128128
"DictionaryLiteral": run_DictionaryLiteral,
129129
"DictionaryRemove": run_DictionaryRemove,
130+
"DictionaryRemoveOfObjects": run_DictionaryRemoveOfObjects,
130131
"DictionarySwap": run_DictionarySwap,
131132
"ErrorHandling": run_ErrorHandling,
132133
"GlobalClass": run_GlobalClass,

0 commit comments

Comments
 (0)