@@ -17,29 +17,50 @@ import TestsUtils
17
17
let t : [ BenchmarkCategory ] = [ . validation, . api, . Array]
18
18
public let ArrayAppend = [
19
19
BenchmarkInfo ( name: " ArrayAppend " , runFunction: run_ArrayAppend, tags: t) ,
20
- BenchmarkInfo ( name: " ArrayAppendArrayOfInt " , runFunction: run_ArrayAppendArrayOfInt, tags: t) ,
20
+ BenchmarkInfo ( name: " ArrayAppendArrayOfInt " , runFunction: run_ArrayAppendArrayOfInt, tags: t,
21
+ setUpFunction: ones, tearDownFunction: releaseOnes) ,
21
22
BenchmarkInfo ( name: " ArrayAppendAscii " , runFunction: run_ArrayAppendAscii, tags: t) ,
22
23
BenchmarkInfo ( name: " ArrayAppendAsciiSubstring " , runFunction: run_ArrayAppendAsciiSubstring, tags: t) ,
23
- BenchmarkInfo ( name: " ArrayAppendFromGeneric " , runFunction: run_ArrayAppendFromGeneric, tags: t) ,
24
- BenchmarkInfo ( name: " ArrayAppendGenericStructs " , runFunction: run_ArrayAppendGenericStructs, tags: t) ,
24
+ BenchmarkInfo ( name: " ArrayAppendFromGeneric " , runFunction: run_ArrayAppendFromGeneric, tags: t,
25
+ setUpFunction: ones, tearDownFunction: releaseOnes) ,
26
+ BenchmarkInfo ( name: " ArrayAppendGenericStructs " , runFunction: run_ArrayAppendGenericStructs, tags: t,
27
+ setUpFunction: { otherStructs = Array ( repeating: S ( x: 3 , y: 4.2 ) , count: 10_000 ) } ,
28
+ tearDownFunction: { otherStructs = nil } ) ,
25
29
BenchmarkInfo ( name: " ArrayAppendLatin1 " , runFunction: run_ArrayAppendLatin1, tags: t) ,
26
30
BenchmarkInfo ( name: " ArrayAppendLatin1Substring " , runFunction: run_ArrayAppendLatin1Substring, tags: t) ,
27
- BenchmarkInfo ( name: " ArrayAppendLazyMap " , runFunction: run_ArrayAppendLazyMap, tags: t) ,
28
- BenchmarkInfo ( name: " ArrayAppendOptionals " , runFunction: run_ArrayAppendOptionals, tags: t) ,
31
+ BenchmarkInfo ( name: " ArrayAppendLazyMap " , runFunction: run_ArrayAppendLazyMap, tags: t,
32
+ setUpFunction: { blackHole ( array) } ) ,
33
+ BenchmarkInfo ( name: " ArrayAppendOptionals " , runFunction: run_ArrayAppendOptionals, tags: t,
34
+ setUpFunction: { otherOptionalOnes = Array ( repeating: 1 , count: 10_000 ) } ,
35
+ tearDownFunction: { otherOptionalOnes = nil } ) ,
29
36
BenchmarkInfo ( name: " ArrayAppendRepeatCol " , runFunction: run_ArrayAppendRepeatCol, tags: t) ,
30
37
BenchmarkInfo ( name: " ArrayAppendReserved " , runFunction: run_ArrayAppendReserved, tags: t) ,
31
38
BenchmarkInfo ( name: " ArrayAppendSequence " , runFunction: run_ArrayAppendSequence, tags: t) ,
32
- BenchmarkInfo ( name: " ArrayAppendStrings " , runFunction: run_ArrayAppendStrings, tags: t) ,
33
- BenchmarkInfo ( name: " ArrayAppendToFromGeneric " , runFunction: run_ArrayAppendToFromGeneric, tags: t) ,
34
- BenchmarkInfo ( name: " ArrayAppendToGeneric " , runFunction: run_ArrayAppendToGeneric, tags: t) ,
39
+ BenchmarkInfo ( name: " ArrayAppendStrings " , runFunction: run_ArrayAppendStrings, tags: t,
40
+ setUpFunction: { otherStrings = stride ( from: 0 , to: 10_000 , by: 1 ) . map { " \( $0) " } } ,
41
+ tearDownFunction: { otherStrings = nil } ) ,
42
+ BenchmarkInfo ( name: " ArrayAppendToFromGeneric " , runFunction: run_ArrayAppendToFromGeneric, tags: t,
43
+ setUpFunction: ones, tearDownFunction: releaseOnes) ,
44
+ BenchmarkInfo ( name: " ArrayAppendToGeneric " , runFunction: run_ArrayAppendToGeneric, tags: t,
45
+ setUpFunction: ones, tearDownFunction: releaseOnes) ,
35
46
BenchmarkInfo ( name: " ArrayAppendUTF16 " , runFunction: run_ArrayAppendUTF16, tags: t) ,
36
47
BenchmarkInfo ( name: " ArrayAppendUTF16Substring " , runFunction: run_ArrayAppendUTF16Substring, tags: t) ,
37
- BenchmarkInfo ( name: " ArrayPlusEqualArrayOfInt " , runFunction: run_ArrayPlusEqualArrayOfInt, tags: t) ,
48
+ BenchmarkInfo ( name: " ArrayPlusEqualArrayOfInt " , runFunction: run_ArrayPlusEqualArrayOfInt, tags: t,
49
+ setUpFunction: ones, tearDownFunction: releaseOnes) ,
38
50
BenchmarkInfo ( name: " ArrayPlusEqualFiveElementCollection " , runFunction: run_ArrayPlusEqualFiveElementCollection, tags: t) ,
39
51
BenchmarkInfo ( name: " ArrayPlusEqualSingleElementCollection " , runFunction: run_ArrayPlusEqualSingleElementCollection, tags: t) ,
40
52
BenchmarkInfo ( name: " ArrayPlusEqualThreeElements " , runFunction: run_ArrayPlusEqualThreeElements, tags: t) ,
41
53
]
42
54
55
+ var otherOnes : [ Int ] !
56
+ var otherOptionalOnes : [ Int ? ] !
57
+ var otherStrings : [ String ] !
58
+ var otherStructs : [ S < Int , Double > ] !
59
+ let array = Array ( 0 ..< 10_000 )
60
+
61
+ func ones( ) { otherOnes = Array ( repeating: 1 , count: 10_000 ) }
62
+ func releaseOnes( ) { otherOnes = nil }
63
+
43
64
// Append single element
44
65
@inline ( never)
45
66
public func run_ArrayAppend( _ N: Int ) {
@@ -87,7 +108,8 @@ public func run_ArrayAppendSequence(_ N: Int) {
87
108
// can pre-reserve capacity.
88
109
@inline ( never)
89
110
public func run_ArrayAppendArrayOfInt( _ N: Int ) {
90
- let other = Array ( repeating: 1 , count: 10_000 )
111
+ let other : [ Int ] = otherOnes
112
+
91
113
for _ in 0 ..< N {
92
114
for _ in 0 ..< 10 {
93
115
var nums = [ Int] ( )
@@ -102,7 +124,8 @@ public func run_ArrayAppendArrayOfInt(_ N: Int) {
102
124
// except +=, to check equally performant.
103
125
@inline ( never)
104
126
public func run_ArrayPlusEqualArrayOfInt( _ N: Int ) {
105
- let other = Array ( repeating: 1 , count: 10_000 )
127
+ let other : [ Int ] = otherOnes
128
+
106
129
for _ in 0 ..< N {
107
130
for _ in 0 ..< 10 {
108
131
var nums = [ Int] ( )
@@ -117,7 +140,8 @@ public func run_ArrayPlusEqualArrayOfInt(_ N: Int) {
117
140
// can pre-reserve capacity.
118
141
@inline ( never)
119
142
public func run_ArrayAppendStrings( _ N: Int ) {
120
- let other = stride ( from: 0 , to: 10_000 , by: 1 ) . map { " \( $0) " }
143
+ let other : [ String ] = otherStrings
144
+
121
145
for _ in 0 ..< N {
122
146
for _ in 0 ..< 10 {
123
147
var nums = [ String] ( )
@@ -138,7 +162,8 @@ struct S<T,U> {
138
162
// can pre-reserve capacity.
139
163
@inline ( never)
140
164
public func run_ArrayAppendGenericStructs( _ N: Int ) {
141
- let other = Array ( repeating: S ( x: 3 , y: 4.2 ) , count: 10_000 )
165
+ let other : [ S < Int , Double > ] = otherStructs
166
+
142
167
for _ in 0 ..< N {
143
168
for _ in 0 ..< 10 {
144
169
var nums = [ S < Int , Double > ] ( )
@@ -153,7 +178,7 @@ public func run_ArrayAppendGenericStructs(_ N: Int) {
153
178
// can pre-reserve capacity.
154
179
@inline ( never)
155
180
public func run_ArrayAppendOptionals( _ N: Int ) {
156
- let other : [ Int ? ] = Array ( repeating : 1 , count : 10_000 )
181
+ let other : [ Int ? ] = otherOptionalOnes
157
182
158
183
for _ in 0 ..< N {
159
184
for _ in 0 ..< 10 {
@@ -170,7 +195,7 @@ public func run_ArrayAppendOptionals(_ N: Int) {
170
195
// can pre-reserve capacity, but no optimization points used.
171
196
@inline ( never)
172
197
public func run_ArrayAppendLazyMap( _ N: Int ) {
173
- let other = Array ( 0 ..< 10_000 ) . lazy. map { $0 * 2 }
198
+ let other = array . lazy. map { $0 * 2 }
174
199
175
200
for _ in 0 ..< N {
176
201
for _ in 0 ..< 10 {
@@ -210,7 +235,7 @@ public func appendFromGeneric<
210
235
211
236
@inline ( never)
212
237
public func run_ArrayAppendFromGeneric( _ N: Int ) {
213
- let other = Array ( repeating : 1 , count : 10_000 )
238
+ let other : [ Int ] = otherOnes
214
239
215
240
for _ in 0 ..< N {
216
241
for _ in 0 ..< 10 {
@@ -232,7 +257,7 @@ public func appendToGeneric<
232
257
233
258
@inline ( never)
234
259
public func run_ArrayAppendToGeneric( _ N: Int ) {
235
- let other = Array ( repeating : 1 , count : 10_000 )
260
+ let other : [ Int ] = otherOnes
236
261
237
262
for _ in 0 ..< N {
238
263
for _ in 0 ..< 10 {
@@ -256,7 +281,7 @@ where R.Element == S.Element {
256
281
257
282
@inline ( never)
258
283
public func run_ArrayAppendToFromGeneric( _ N: Int ) {
259
- let other = Array ( repeating : 1 , count : 10_000 )
284
+ let other : [ Int ] = otherOnes
260
285
261
286
for _ in 0 ..< N {
262
287
for _ in 0 ..< 10 {
0 commit comments