|
| 1 | +import TestsUtils |
| 2 | + |
| 3 | +// This benchmark aims to measuare heapSort path of stdlib sorting function. |
| 4 | +// Datasets in this benchmark are influenced by stdlib partition function, |
| 5 | +// therefore if stdlib partion implementation changes we should correct these |
| 6 | +// datasets or disable/skip this benchmark |
| 7 | +public let SortIntPyramids = [ |
| 8 | + BenchmarkInfo( |
| 9 | + name: "SortIntPyramid", |
| 10 | + runFunction: run_SortIntPyramid, |
| 11 | + tags: [.validation, .api, .algorithm]), |
| 12 | + BenchmarkInfo( |
| 13 | + name: "SortAdjacentIntPyramids", |
| 14 | + runFunction: run_SortAdjacentIntPyramids, |
| 15 | + tags: [.validation, .api, .algorithm]), |
| 16 | +] |
| 17 | + |
| 18 | +// let A - array sorted in ascending order, |
| 19 | +// A^R - reversed array A, + - array concatenation operator |
| 20 | +// A indices are in range 1...A.length |
| 21 | +// define the pyramid as A + A^R |
| 22 | +// define pyramid height as A[A.length] |
| 23 | + |
| 24 | +// On 92% of following dataset stdlib sorting function will use heapSort. |
| 25 | +// number of ranges sorted by heapSort: 26 |
| 26 | +// median heapSort range length: 198 |
| 27 | +// maximum -||-: 1774 |
| 28 | +// average -||-: 357 |
| 29 | + |
| 30 | +// pyramid height |
| 31 | +let pH = 5000 |
| 32 | +let pyramidTemplate: [Int] = (1...pH) + (1...pH).reversed() |
| 33 | + |
| 34 | +// let A - array sorted in ascending order, |
| 35 | +// A^R - reversed array A, + - array concatenation operator, |
| 36 | +// A indices are in range 1...A.length. |
| 37 | +// define adjacent pyramid as A + A^R + A + A^R, |
| 38 | +// defne adjacent pyramid hight as A[A.length]. |
| 39 | + |
| 40 | + |
| 41 | +// On 25% of following dataset stdlib sorting function will use heapSort. |
| 42 | +// number of ranges sorted by heapSort: 71 |
| 43 | +// median heapSort range length: 28 |
| 44 | +// maximum -||-: 120 |
| 45 | +// average -||-: 36 |
| 46 | + |
| 47 | +// adjacent pyramids height. |
| 48 | +let aPH = pH / 2 |
| 49 | +let adjacentPyramidsTemplate: [Int] = (1...aPH) + (1...aPH).reversed() |
| 50 | + + (1...aPH) + (1...aPH).reversed() |
| 51 | + |
| 52 | +@inline(never) |
| 53 | +public func run_SortIntPyramid(_ N: Int) { |
| 54 | + for _ in 1...25*N { |
| 55 | + var pyramid = pyramidTemplate |
| 56 | + |
| 57 | + // sort pyramid in place. |
| 58 | + pyramid.sort() |
| 59 | + |
| 60 | + // Check whether pyramid is sorted. |
| 61 | + CheckResults(pyramid[0] <= pyramid[pyramid.count/2]) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +@inline(never) |
| 66 | +public func run_SortAdjacentIntPyramids(_ N: Int) { |
| 67 | + for _ in 1...25*N { |
| 68 | + var adjacentPyramids = adjacentPyramidsTemplate |
| 69 | + adjacentPyramids.sort() |
| 70 | + // Check whether pyramid is sorted. |
| 71 | + CheckResults( |
| 72 | + adjacentPyramids[0] <= adjacentPyramids[adjacentPyramids.count/2]) |
| 73 | + } |
| 74 | +} |
0 commit comments