Skip to content

Commit 63665cd

Browse files
authored
Merge pull request #18667 from overlazy/HeapSortBenchmark
2 parents 7f2fff6 + 248e843 commit 63665cd

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ set(SWIFT_BENCH_MODULES
141141
single-source/SetTests
142142
single-source/SevenBoom
143143
single-source/Sim2DArray
144+
single-source/SortIntPyramids
144145
single-source/SortLargeExistentials
145146
single-source/SortLettersInPlace
146147
single-source/SortStrings
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ import SequenceAlgos
132132
import SetTests
133133
import SevenBoom
134134
import Sim2DArray
135+
import SortIntPyramids
135136
import SortLargeExistentials
136137
import SortLettersInPlace
137138
import SortStrings
@@ -296,6 +297,7 @@ registerBenchmark(SequenceAlgos)
296297
registerBenchmark(SetTests)
297298
registerBenchmark(SevenBoom)
298299
registerBenchmark(Sim2DArray)
300+
registerBenchmark(SortIntPyramids)
299301
registerBenchmark(SortLargeExistentials)
300302
registerBenchmark(SortLettersInPlace)
301303
registerBenchmark(SortStrings)

0 commit comments

Comments
 (0)