Skip to content

Commit f6a1926

Browse files
committed
[benchmark] Add Dictionary3OfObjects
Another dictionary benchmark with objects as key and value types.
1 parent 577b0f2 commit f6a1926

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

benchmark/single-source/DictTest3.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,54 @@ public func run_Dictionary3(N: Int) {
4343
}
4444
CheckResults(res == ref_result, "Incorrect results in Dictionary3: \(res) != \(ref_result)")
4545
}
46+
47+
class Box<T : Hashable where T : Equatable> : Hashable {
48+
var value: T
49+
50+
init(_ v: T) {
51+
value = v
52+
}
53+
54+
var hashValue : Int {
55+
return value.hashValue
56+
}
57+
}
58+
59+
extension Box : Equatable {
60+
}
61+
62+
func ==<T: Equatable>(lhs: Box<T>, rhs: Box<T>) -> Bool {
63+
return lhs.value == rhs.value
64+
}
65+
66+
@inline(never)
67+
public func run_Dictionary3OfObjects(N: Int) {
68+
let size1 = 100
69+
let reps = 20
70+
let ref_result = "1 99 20 1980"
71+
var hash1 : [ Box<String> : Box<Int> ] = [:]
72+
var hash2 : [ Box<String> : Box<Int> ] = [:]
73+
var res = ""
74+
75+
for _ in 1...N {
76+
hash1 = [:]
77+
for i in 0..<size1 {
78+
hash1[Box("foo_" + String(i))] = Box(i)
79+
}
80+
81+
hash2 = hash1
82+
83+
for _ in 1..<reps {
84+
for (k, v) in hash1 {
85+
hash2[k] = Box(hash2[k]!.value + v.value)
86+
}
87+
}
88+
89+
res = (String(hash1[Box("foo_1")]!.value) + " " + String(hash1[Box("foo_99")]!.value) + " " +
90+
String(hash2[Box("foo_1")]!.value) + " " + String(hash2[Box("foo_99")]!.value))
91+
if res != ref_result {
92+
break
93+
}
94+
}
95+
CheckResults(res == ref_result, "Incorrect results in Dictionary3OfObject: \(res) != \(ref_result)")
96+
}

benchmark/utils/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ precommitTests = [
123123
"Dictionary2": run_Dictionary2,
124124
"Dictionary2OfObjects": run_Dictionary2OfObjects,
125125
"Dictionary3": run_Dictionary3,
126+
"Dictionary3OfObjects": run_Dictionary3OfObjects,
126127
"DictionaryBridge": run_DictionaryBridge,
127128
"DictionaryLiteral": run_DictionaryLiteral,
128129
"DictionaryRemove": run_DictionaryRemove,

0 commit comments

Comments
 (0)