@@ -42,15 +42,14 @@ public func run_ArrayAppendReserved(_ N: Int) {
42
42
}
43
43
44
44
// Append a sequence. Length of sequence unknown so
45
- // can't pre-reserve capacity. Should be comparable
46
- // to append single elements.
45
+ // can't pre-reserve capacity.
47
46
@inline ( never)
48
47
public func run_ArrayAppendSequence( _ N: Int ) {
49
48
let seq = stride ( from: 0 , to: 10_000 , by: 1 )
50
49
for _ in 0 ..< N {
51
50
for _ in 0 ..< 10 {
52
51
var nums = [ Int] ( )
53
- for _ in 0 ..< 4 {
52
+ for _ in 0 ..< 8 {
54
53
nums. append ( contentsOf: seq)
55
54
}
56
55
}
@@ -60,16 +59,102 @@ public func run_ArrayAppendSequence(_ N: Int) {
60
59
// Append another array. Length of sequence known so
61
60
// can pre-reserve capacity.
62
61
@inline ( never)
63
- public func run_ArrayAppendArray ( _ N: Int ) {
62
+ public func run_ArrayAppendArrayOfInt ( _ N: Int ) {
64
63
let other = Array ( repeating: 1 , count: 10_000 )
65
64
for _ in 0 ..< N {
66
65
for _ in 0 ..< 10 {
67
66
var nums = [ Int] ( )
67
+ for _ in 0 ..< 8 {
68
+ nums += other
69
+ }
70
+ }
71
+ }
72
+ }
73
+
74
+ // Append another array. Length of sequence known so
75
+ // can pre-reserve capacity.
76
+ @inline ( never)
77
+ public func run_ArrayAppendStrings( _ N: Int ) {
78
+ let other = stride ( from: 0 , to: 10_000 , by: 1 ) . map { " \( $0) " }
79
+ for _ in 0 ..< N {
80
+ for _ in 0 ..< 10 {
81
+ var nums = [ String] ( )
82
+ // lower inner count due to string slowness
68
83
for _ in 0 ..< 4 {
69
- // note, this uses += rather than append(contentsOf:),
70
- // to ensure operator doesn't introduce inefficiency
71
84
nums += other
72
85
}
73
86
}
74
87
}
75
88
}
89
+
90
+ struct S < T, U> {
91
+ var x : T
92
+ var y : U
93
+ }
94
+
95
+ // Append another array. Length of sequence known so
96
+ // can pre-reserve capacity.
97
+ @inline ( never)
98
+ public func run_ArrayAppendGenericStructs( _ N: Int ) {
99
+ let other = Array ( repeating: S ( x: 3 , y: 4.2 ) , count: 10_000 )
100
+ for _ in 0 ..< N {
101
+ for _ in 0 ..< 10 {
102
+ var nums = [ S < Int , Double > ] ( )
103
+ for _ in 0 ..< 8 {
104
+ nums += other
105
+ }
106
+ }
107
+ }
108
+ }
109
+
110
+ // Append another array. Length of sequence known so
111
+ // can pre-reserve capacity.
112
+ @inline ( never)
113
+ public func run_ArrayAppendOptionals( _ N: Int ) {
114
+ let other : [ Int ? ] = Array ( repeating: 1 , count: 10_000 )
115
+
116
+ for _ in 0 ..< N {
117
+ for _ in 0 ..< 10 {
118
+ var nums = [ Int? ] ( )
119
+ for _ in 0 ..< 8 {
120
+ nums += other
121
+ }
122
+ }
123
+ }
124
+ }
125
+
126
+
127
+ // Append a lazily-mapped array. Length of sequence known so
128
+ // can pre-reserve capacity, but no optimization points used.
129
+ @inline ( never)
130
+ public func run_ArrayAppendLazyMap( _ N: Int ) {
131
+ let other = Array ( 0 ..< 10_000 ) . lazy. map { $0 * 2 }
132
+
133
+ for _ in 0 ..< N {
134
+ for _ in 0 ..< 10 {
135
+ var nums = [ Int] ( )
136
+ for _ in 0 ..< 8 {
137
+ nums += other
138
+ }
139
+ }
140
+ }
141
+ }
142
+
143
+
144
+ // Append a Repeat collection. Length of sequence known so
145
+ // can pre-reserve capacity, but no optimization points used.
146
+ @inline ( never)
147
+ public func run_ArrayAppendRepeatCol( _ N: Int ) {
148
+ let other = repeatElement ( 1 , count: 10_000 )
149
+
150
+ for _ in 0 ..< N {
151
+ for _ in 0 ..< 10 {
152
+ var nums = [ Int] ( )
153
+ for _ in 0 ..< 8 {
154
+ nums += other
155
+ }
156
+ }
157
+ }
158
+ }
159
+
160
+
0 commit comments