Skip to content

Commit 577b0f2

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

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

benchmark/single-source/DictTest2.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,47 @@ public func run_Dictionary2(N: Int) {
3636
}
3737
CheckResults(res == ref_result, "Incorrect results in Dictionary2: \(res) != \(ref_result)")
3838
}
39+
40+
class Box<T : Hashable where T : Equatable> : Hashable {
41+
var value: T
42+
43+
init(_ v: T) {
44+
value = v
45+
}
46+
47+
var hashValue : Int {
48+
return value.hashValue
49+
}
50+
}
51+
52+
extension Box : Equatable {
53+
}
54+
55+
func ==<T: Equatable>(lhs: Box<T>, rhs: Box<T>) -> Bool {
56+
return lhs.value == rhs.value
57+
}
58+
59+
@inline(never)
60+
public func run_Dictionary2OfObjects(N: Int) {
61+
let size = 500
62+
let ref_result = 199
63+
var res = 0
64+
for _ in 1...5*N {
65+
var x: [Box<String>:Box<Int>] = [:]
66+
for i in 1...size {
67+
x[Box(String(i, radix:16))] = Box(i)
68+
}
69+
70+
res = 0
71+
for i in 0..<size {
72+
let i2 = size-i
73+
if x[Box(String(i2))] != nil {
74+
res += 1
75+
}
76+
}
77+
if res != ref_result {
78+
break
79+
}
80+
}
81+
CheckResults(res == ref_result, "Incorrect results in Dictionary2AllObjects: \(res) != \(ref_result)")
82+
}

benchmark/utils/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ precommitTests = [
121121
"Dictionary": run_Dictionary,
122122
"DictionaryOfObjects": run_DictionaryOfObjects,
123123
"Dictionary2": run_Dictionary2,
124+
"Dictionary2OfObjects": run_Dictionary2OfObjects,
124125
"Dictionary3": run_Dictionary3,
125126
"DictionaryBridge": run_DictionaryBridge,
126127
"DictionaryLiteral": run_DictionaryLiteral,

0 commit comments

Comments
 (0)