Skip to content

Commit cdfc31c

Browse files
committed
[benchmark] DictionaryRemove Setup Overhead
1 parent de86e4f commit cdfc31c

File tree

1 file changed

+15
-54
lines changed

1 file changed

+15
-54
lines changed

benchmark/single-source/DictionaryRemove.swift

Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,26 @@ import TestsUtils
1616

1717
let t: [BenchmarkCategory] = [.validation, .api, .Dictionary]
1818

19+
let size = 100
20+
let numberMap = Dictionary(uniqueKeysWithValues: zip(1...size, 1...size))
21+
let boxedNums = (1...size).lazy.map { Box($0) }
22+
let boxedNumMap = Dictionary(uniqueKeysWithValues: zip(boxedNums, boxedNums))
23+
1924
public let DictionaryRemove = [
2025
BenchmarkInfo(name: "DictionaryRemove",
21-
runFunction: run_DictionaryRemove, tags: t, legacyFactor: 10),
26+
runFunction: { for _ in 1...$0*100 {
27+
var dict = numberMap
28+
for i in 1...size { dict.removeValue(forKey: i) }
29+
CheckResults(dict.isEmpty)
30+
}}, tags: t, legacyFactor: 10),
2231
BenchmarkInfo(name: "DictionaryRemoveOfObjects",
23-
runFunction: run_DictionaryRemoveOfObjects, tags: t, legacyFactor: 100),
32+
runFunction: { for _ in 1...$0*10 {
33+
var dict = boxedNumMap
34+
for i in 1...size { dict.removeValue(forKey: Box(i)) }
35+
CheckResults(dict.isEmpty)
36+
}}, tags: t, legacyFactor: 100),
2437
]
2538

26-
@inline(never)
27-
public func run_DictionaryRemove(_ N: Int) {
28-
let size = 100
29-
var dict = [Int: Int](minimumCapacity: size)
30-
31-
// Fill dictionary
32-
for i in 1...size {
33-
dict[i] = i
34-
}
35-
CheckResults(dict.count == size)
36-
37-
var tmpDict = dict
38-
for _ in 1...100*N {
39-
tmpDict = dict
40-
// Empty dictionary
41-
for i in 1...size {
42-
tmpDict.removeValue(forKey: i)
43-
}
44-
if !tmpDict.isEmpty {
45-
break
46-
}
47-
}
48-
49-
CheckResults(tmpDict.isEmpty)
50-
}
51-
5239
class Box<T : Hashable> : Hashable {
5340
var value: T
5441

@@ -64,29 +51,3 @@ class Box<T : Hashable> : Hashable {
6451
return lhs.value == rhs.value
6552
}
6653
}
67-
68-
@inline(never)
69-
public func run_DictionaryRemoveOfObjects(_ N: Int) {
70-
let size = 100
71-
var dict = Dictionary<Box<Int>, Box<Int>>(minimumCapacity: size)
72-
73-
// Fill dictionary
74-
for i in 1...size {
75-
dict[Box(i)] = Box(i)
76-
}
77-
CheckResults(dict.count == size)
78-
79-
var tmpDict = dict
80-
for _ in 1...10*N {
81-
tmpDict = dict
82-
// Empty dictionary
83-
for i in 1...size {
84-
tmpDict.removeValue(forKey: Box(i))
85-
}
86-
if !tmpDict.isEmpty {
87-
break
88-
}
89-
}
90-
91-
CheckResults(tmpDict.isEmpty)
92-
}

0 commit comments

Comments
 (0)