|
| 1 | +// This test checks the performance of removeAll on a non-uniquely referenced array. |
| 2 | +import TestsUtils |
| 3 | + |
| 4 | +public let benchmarks = [ |
| 5 | + BenchmarkInfo( |
| 6 | + name: "ArrayRemoveAll_Class", |
| 7 | + runFunction: run_ArrayRemoveAll_Class, |
| 8 | + tags: [.validation, .api, .Array], |
| 9 | + setUpFunction: { blackHole(inputArray_Class) }, |
| 10 | + tearDownFunction: { inputArray_Class = nil } |
| 11 | + ), |
| 12 | + BenchmarkInfo( |
| 13 | + name: "ArrayRemoveAll_Int", |
| 14 | + runFunction: run_ArrayRemoveAll_Int, |
| 15 | + tags: [.validation, .api, .Array], |
| 16 | + setUpFunction: { blackHole(inputArray_Int) }, |
| 17 | + tearDownFunction: { inputArray_Int = nil } |
| 18 | + ) |
| 19 | +] |
| 20 | + |
| 21 | +let size_Int = 1_000_000 |
| 22 | +let size_Class = 100_000 |
| 23 | + |
| 24 | +var inputArray_Int: [Int]! = { |
| 25 | + var a: [Int] = [] |
| 26 | + a.reserveCapacity(size_Int) |
| 27 | + for i in 0 ..< size_Int { |
| 28 | + a.append(i) |
| 29 | + } |
| 30 | + return a |
| 31 | +}() |
| 32 | + |
| 33 | +var inputArray_Class: [Slow]! = { |
| 34 | + var a: [Slow] = [] |
| 35 | + a.reserveCapacity(size_Class) |
| 36 | + for i in 0 ..< size_Class { |
| 37 | + a.append(Slow(num: i)) |
| 38 | + } |
| 39 | + return a |
| 40 | +}() |
| 41 | + |
| 42 | +class Slow { |
| 43 | + public var num: Int |
| 44 | + |
| 45 | + init(num: Int) { |
| 46 | + self.num = num |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +@inline(never) |
| 52 | +func verifyCapacity<T>(_ new: [T], orig: [T]) -> Bool { |
| 53 | + return new.capacity == orig.capacity |
| 54 | +} |
| 55 | + |
| 56 | +@inline(never) |
| 57 | +func removeAll<T>(_ arr: [T]) -> [T] { |
| 58 | + var copy = arr |
| 59 | + copy.removeAll(keepingCapacity: true) |
| 60 | + return copy |
| 61 | +} |
| 62 | + |
| 63 | +@inline(never) |
| 64 | +func copyItem<T>(_ item: T) -> T { |
| 65 | + return item |
| 66 | +} |
| 67 | + |
| 68 | +@inline(never) |
| 69 | +func run_ArrayRemoveAll_Class(_ n: Int) { |
| 70 | + for _ in 1...n { |
| 71 | + let copy = removeAll(inputArray_Class) |
| 72 | + check(verifyCapacity(copy, orig: inputArray_Class)) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +@inline(never) |
| 77 | +func run_ArrayRemoveAll_Int(_ n: Int) { |
| 78 | + for _ in 1...n { |
| 79 | + let copy = removeAll(inputArray_Int) |
| 80 | + check(verifyCapacity(copy, orig: inputArray_Int)) |
| 81 | + } |
| 82 | +} |
0 commit comments