|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift Collections open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2021 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +import CollectionsBenchmark |
| 13 | +import SortedCollections |
| 14 | + |
| 15 | +extension Benchmark { |
| 16 | + public mutating func addSortedSetBenchmarks() { |
| 17 | + self.addSimple( |
| 18 | + title: "SortedSet<Int> init from range", |
| 19 | + input: Int.self |
| 20 | + ) { size in |
| 21 | + blackHole(SortedSet(0 ..< size)) |
| 22 | + } |
| 23 | + |
| 24 | + self.addSimple( |
| 25 | + title: "SortedSet<Int> init(sortedElements:) from range", |
| 26 | + input: Int.self |
| 27 | + ) { size in |
| 28 | + blackHole(SortedSet(sortedElements: 0 ..< size)) |
| 29 | + } |
| 30 | + self.add( |
| 31 | + title: "SortedSet<Int> sequential iteration", |
| 32 | + input: [Int].self |
| 33 | + ) { input in |
| 34 | + let set = SortedSet(input) |
| 35 | + return { timer in |
| 36 | + for i in set { |
| 37 | + blackHole(i) |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + self.add( |
| 43 | + title: "SortedSet<Int> forEach iteration", |
| 44 | + input: [Int].self |
| 45 | + ) { input in |
| 46 | + let set = SortedSet(input) |
| 47 | + return { timer in |
| 48 | + set.forEach { i in |
| 49 | + blackHole(i) |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + self.add( |
| 55 | + title: "SortedSet<Int> successful contains", |
| 56 | + input: ([Int], [Int]).self |
| 57 | + ) { input, lookups in |
| 58 | + let set = SortedSet(input) |
| 59 | + return { timer in |
| 60 | + for i in lookups { |
| 61 | + precondition(set.contains(i)) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + self.add( |
| 67 | + title: "SortedSet<Int> unsuccessful contains", |
| 68 | + input: ([Int], [Int]).self |
| 69 | + ) { input, lookups in |
| 70 | + let set = SortedSet(input) |
| 71 | + let lookups = lookups.map { $0 + input.count } |
| 72 | + return { timer in |
| 73 | + for i in lookups { |
| 74 | + precondition(!set.contains(i)) |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + self.addSimple( |
| 80 | + title: "SortedSet<Int> insertions", |
| 81 | + input: [Int].self |
| 82 | + ) { input in |
| 83 | + var set: SortedSet<Int> = [] |
| 84 | + for i in input { |
| 85 | + set.insert(i) |
| 86 | + } |
| 87 | + precondition(set.count == input.count) |
| 88 | + blackHole(set) |
| 89 | + } |
| 90 | + |
| 91 | + self.add( |
| 92 | + title: "SortedSet<Int> remove", |
| 93 | + input: ([Int], [Int]).self |
| 94 | + ) { input, removals in |
| 95 | + return { timer in |
| 96 | + var set = SortedSet(input) |
| 97 | + timer.measure { |
| 98 | + for i in removals { |
| 99 | + set.remove(i) |
| 100 | + } |
| 101 | + } |
| 102 | + precondition(set.isEmpty) |
| 103 | + blackHole(set) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + self.add( |
| 108 | + title: "SortedSet<Int> removeLast", |
| 109 | + input: Int.self |
| 110 | + ) { size in |
| 111 | + return { timer in |
| 112 | + var set = SortedSet(0 ..< size) |
| 113 | + timer.measure { |
| 114 | + for _ in 0 ..< size { |
| 115 | + set.removeLast() |
| 116 | + } |
| 117 | + } |
| 118 | + precondition(set.isEmpty) |
| 119 | + blackHole(set) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + self.add( |
| 124 | + title: "SortedSet<Int> removeFirst", |
| 125 | + input: Int.self |
| 126 | + ) { size in |
| 127 | + return { timer in |
| 128 | + var set = SortedSet(0 ..< size) |
| 129 | + timer.measure { |
| 130 | + for _ in 0 ..< size { |
| 131 | + set.removeFirst() |
| 132 | + } |
| 133 | + } |
| 134 | + precondition(set.isEmpty) |
| 135 | + blackHole(set) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + let overlaps: [(String, (Int) -> Int)] = [ |
| 140 | + ("0%", { c in c }), |
| 141 | + ("25%", { c in 3 * c / 4 }), |
| 142 | + ("50%", { c in c / 2 }), |
| 143 | + ("75%", { c in c / 4 }), |
| 144 | + ("100%", { c in 0 }), |
| 145 | + ] |
| 146 | + |
| 147 | + // SetAlgebra operations with Self |
| 148 | + do { |
| 149 | + for (percentage, start) in overlaps { |
| 150 | + self.add( |
| 151 | + title: "SortedSet<Int> union with Self (\(percentage) overlap)", |
| 152 | + input: [Int].self |
| 153 | + ) { input in |
| 154 | + let start = start(input.count) |
| 155 | + let a = SortedSet(input) |
| 156 | + let b = SortedSet(start ..< start + input.count) |
| 157 | + return { timer in |
| 158 | + blackHole(a.union(b)) |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + for (percentage, start) in overlaps { |
| 164 | + self.add( |
| 165 | + title: "SortedSet<Int> intersection with Self (\(percentage) overlap)", |
| 166 | + input: [Int].self |
| 167 | + ) { input in |
| 168 | + let start = start(input.count) |
| 169 | + let a = SortedSet(input) |
| 170 | + let b = SortedSet(start ..< start + input.count) |
| 171 | + return { timer in |
| 172 | + blackHole(a.intersection(b)) |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + for (percentage, start) in overlaps { |
| 178 | + self.add( |
| 179 | + title: "SortedSet<Int> symmetricDifference with Self (\(percentage) overlap)", |
| 180 | + input: [Int].self |
| 181 | + ) { input in |
| 182 | + let start = start(input.count) |
| 183 | + let a = SortedSet(input) |
| 184 | + let b = SortedSet(start ..< start + input.count) |
| 185 | + return { timer in |
| 186 | + blackHole(a.symmetricDifference(b)) |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + for (percentage, start) in overlaps { |
| 192 | + self.add( |
| 193 | + title: "SortedSet<Int> subtracting Self (\(percentage) overlap)", |
| 194 | + input: [Int].self |
| 195 | + ) { input in |
| 196 | + let start = start(input.count) |
| 197 | + let a = SortedSet(input) |
| 198 | + let b = SortedSet(start ..< start + input.count) |
| 199 | + return { timer in |
| 200 | + blackHole(a.subtracting(b)) |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + } |
| 207 | +} |
0 commit comments