-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathIndexPathTest.swift
161 lines (138 loc) · 3.84 KB
/
IndexPathTest.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//===--- IndexPathTest.swift ----------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import TestsUtils
import Foundation
let size = 200
let tags: [BenchmarkCategory] = [.validation, .api, .IndexPath]
public let IndexPathTest = [
BenchmarkInfo(name: "IndexPathSubscriptMutation",
runFunction: { n in run_IndexPathSubscriptMutation(n * 1000, size)},
tags: tags),
BenchmarkInfo(name: "IndexPathSubscriptRangeMutation",
runFunction: { n in run_IndexPathSubscriptRangeMutation(n * 1000, size)},
tags: tags),
BenchmarkInfo(name: "IndexPathMaxBeginning",
runFunction: { n in run_IndexPathMaxBeginning(n * 1000)},
tags: tags),
BenchmarkInfo(name: "IndexPathMaxMiddle",
runFunction: { n in run_IndexPathMaxMiddle(n * 1000)},
tags: tags),
BenchmarkInfo(name: "IndexPathMaxEnd",
runFunction: { n in run_IndexPathMaxEnd(n * 1000)},
tags: tags),
BenchmarkInfo(name: "IndexPathMinBeginning",
runFunction: { n in run_IndexPathMinBeginning(n * 1000)},
tags: tags),
BenchmarkInfo(name: "IndexPathMinMiddle",
runFunction: { n in run_IndexPathMinMiddle(n * 1000)},
tags: tags),
BenchmarkInfo(name: "IndexPathMinEnd",
runFunction: { n in run_IndexPathMinEnd(n * 1000)},
tags: tags),
]
@inline(__always)
func indexPath(_ size: Int, reversed: Bool = false) -> IndexPath {
let indexes = Array(0..<size)
return IndexPath(indexes: reversed ? indexes.reversed() : indexes)
}
@inline(__always)
func indexPath(_ size: Int, middle: Int) -> IndexPath {
var indexes = Array(0..<size)
indexes.insert(middle, at: (indexes.count - 1) / 2)
return IndexPath(indexes: indexes)
}
// Subscript Mutations
@inline(__always)
func subscriptMutation(n: Int,
mutations: Int,
mutate: (inout IndexPath, Int) -> ()) {
for _ in 0..<n {
var ip = indexPath(size)
for i in 0..<mutations {
mutate(&ip, i)
}
blackHole(ip)
}
}
@inline(never)
public func run_IndexPathSubscriptMutation(_ n: Int, _ count: Int) {
subscriptMutation(n: n, mutations: count, mutate: { ip, i in
ip[i % 4] += 1
})
}
@inline(never)
public func run_IndexPathSubscriptRangeMutation(_ n: Int, _ count: Int) {
subscriptMutation(n: count, mutations: count, mutate: { ip, i in
ip[0..<i] += [i]
})
}
// Max, Min
@inline(__always)
func maxMin(n: Int,
creator: () -> IndexPath,
maxMinFunc: (inout IndexPath) -> Int?) {
for _ in 0..<n {
var ip = creator()
let found = maxMinFunc(&ip) != nil
blackHole(found)
}
}
// Max
@inline(never)
public func run_IndexPathMaxBeginning(_ n: Int) {
maxMin(n: n, creator: {
indexPath(size, reversed: true)
}, maxMinFunc: { ip in
ip.max()
})
}
@inline(never)
public func run_IndexPathMaxMiddle(_ n: Int) {
maxMin(n: n, creator: {
indexPath(size, middle: size + 1)
}, maxMinFunc: { ip in
ip.max()
})
}
@inline(never)
public func run_IndexPathMaxEnd(_ n: Int) {
maxMin(n: n, creator: {
indexPath(size)
}, maxMinFunc: { ip in
ip.max()
})
}
// Min
@inline(never)
public func run_IndexPathMinBeginning(_ n: Int) {
maxMin(n: n, creator: {
indexPath(size)
}, maxMinFunc: { ip in
ip.min()
})
}
@inline(never)
public func run_IndexPathMinMiddle(_ n: Int) {
maxMin(n: n, creator: {
indexPath(size, middle: -1)
}, maxMinFunc: { ip in
ip.min()
})
}
@inline(never)
public func run_IndexPathMinEnd(_ n: Int) {
maxMin(n: n, creator: {
indexPath(size, reversed: true)
}, maxMinFunc: { ip in
ip.min()
})
}