File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 14
14
15
15
import TestsUtils
16
16
17
+ // Append single element
17
18
@inline ( never)
18
19
public func run_ArrayAppend( _ N: Int ) {
19
20
for _ in 0 ..< N {
@@ -26,6 +27,7 @@ public func run_ArrayAppend(_ N: Int) {
26
27
}
27
28
}
28
29
30
+ // Append single element with reserve
29
31
@inline ( never)
30
32
public func run_ArrayAppendReserved( _ N: Int ) {
31
33
for _ in 0 ..< N {
@@ -38,3 +40,36 @@ public func run_ArrayAppendReserved(_ N: Int) {
38
40
}
39
41
}
40
42
}
43
+
44
+ // Append a sequence. Length of sequence unknown so
45
+ // can't pre-reserve capacity. Should be comparable
46
+ // to append single elements.
47
+ @inline ( never)
48
+ public func run_ArrayAppendSequence( _ N: Int ) {
49
+ let seq = stride ( from: 0 , to: 10_000 , by: 1 )
50
+ for _ in 0 ..< N {
51
+ for _ in 0 ..< 10 {
52
+ var nums = [ Int] ( )
53
+ for _ in 0 ..< 4 {
54
+ nums. append ( contentsOf: seq)
55
+ }
56
+ }
57
+ }
58
+ }
59
+
60
+ // Append another array. Length of sequence known so
61
+ // can pre-reserve capacity.
62
+ @inline ( never)
63
+ public func run_ArrayAppendArray( _ N: Int ) {
64
+ let other = Array ( repeating: 1 , count: 10_000 )
65
+ for _ in 0 ..< N {
66
+ for _ in 0 ..< 10 {
67
+ var nums = [ Int] ( )
68
+ for _ in 0 ..< 4 {
69
+ // note, this uses += rather than append(contentsOf:),
70
+ // to ensure operator doesn't introduce inefficiency
71
+ nums += other
72
+ }
73
+ }
74
+ }
75
+ }
Original file line number Diff line number Diff line change @@ -106,6 +106,8 @@ precommitTests = [
106
106
" Array2D " : run_Array2D,
107
107
" ArrayAppend " : run_ArrayAppend,
108
108
" ArrayAppendReserved " : run_ArrayAppendReserved,
109
+ " ArrayAppendSequence " : run_ArrayAppendSequence,
110
+ " ArrayAppendArray " : run_ArrayAppendArray,
109
111
" ArrayInClass " : run_ArrayInClass,
110
112
" ArrayLiteral " : run_ArrayLiteral,
111
113
" ArrayOfGenericPOD " : run_ArrayOfGenericPOD,
You can’t perform that action at this time.
0 commit comments