|
| 1 | +//===--- SortArrayInClass.swift -------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2019 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 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +// This benchmark is derived from user code that encoutered a major |
| 12 | +// performance problem in normal usage. Contributed by Saleem |
| 13 | +// Abdulrasool (compnerd). |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +import TestsUtils |
| 18 | + |
| 19 | +// Specifically tests efficient access to Array subscript when the |
| 20 | +// array is a class property, but also generally tests quicksort in a |
| 21 | +// class which needs a slew of array optimizations, uniqueness, bounds |
| 22 | +// and exclusivity optimizations. |
| 23 | +public let SortArrayInClass = [ |
| 24 | + BenchmarkInfo( |
| 25 | + name: "SortArrayInClass", |
| 26 | + runFunction: run_SortArrayInClass, |
| 27 | + tags: [.abstraction, .safetychecks, .exclusivity, .algorithm, .api, .Array]) |
| 28 | +] |
| 29 | + |
| 30 | +let LARGE_ARRAY_SIZE = 10000 |
| 31 | + |
| 32 | +class Sorter { |
| 33 | + var array: [Int] |
| 34 | + init(size: Int) { |
| 35 | + array = Array((0..<size).reversed()) |
| 36 | + } |
| 37 | + |
| 38 | + private func _swap(i: Int, j: Int) { |
| 39 | + let t = array[i] |
| 40 | + // This currently copies the entire array. Assigning to a |
| 41 | + // temporary, or using swapAt would avoid the copy, but users |
| 42 | + // shouldn't need to know that. |
| 43 | + array[i] = array[j] |
| 44 | + array[j] = t |
| 45 | + } |
| 46 | + |
| 47 | + private func _quicksort(left: Int, right: Int) { |
| 48 | + |
| 49 | + if left < right { |
| 50 | + let pivot = array[left + ((right - left) / 2)] |
| 51 | + var left_new = left |
| 52 | + var right_new = right |
| 53 | + |
| 54 | + repeat { |
| 55 | + while array[left_new] < pivot { |
| 56 | + left_new += 1 |
| 57 | + } |
| 58 | + while pivot < array[right_new] { |
| 59 | + right_new -= 1 |
| 60 | + } |
| 61 | + if left_new <= right_new { |
| 62 | + _swap(i:left_new, j:right_new) |
| 63 | + left_new += 1 |
| 64 | + right_new -= 1 |
| 65 | + } |
| 66 | + } while left_new <= right_new |
| 67 | + |
| 68 | + _quicksort(left: left, right: right_new) |
| 69 | + _quicksort(left: left_new, right:right) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + func quicksort() { |
| 74 | + _quicksort(left:0, right:array.count - 1); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +public func run_SortArrayInClass(_ N: Int) { |
| 79 | + for _ in 1...N { |
| 80 | + // The array needs to be reinitialized before each sort, so it |
| 81 | + // can't be a setup/tearDown function. |
| 82 | + let sorter = Sorter(size:LARGE_ARRAY_SIZE) |
| 83 | + sorter.quicksort() |
| 84 | + } |
| 85 | +} |
0 commit comments