Skip to content

Commit e4551d7

Browse files
authored
Merge pull request #5436 from airspeedswift/append-contentsOf-benchmark
2 parents 0e40856 + 79f1880 commit e4551d7

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

benchmark/single-source/ArrayAppend.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import TestsUtils
1616

17+
// Append single element
1718
@inline(never)
1819
public func run_ArrayAppend(_ N: Int) {
1920
for _ in 0..<N {
@@ -26,6 +27,7 @@ public func run_ArrayAppend(_ N: Int) {
2627
}
2728
}
2829

30+
// Append single element with reserve
2931
@inline(never)
3032
public func run_ArrayAppendReserved(_ N: Int) {
3133
for _ in 0..<N {
@@ -38,3 +40,36 @@ public func run_ArrayAppendReserved(_ N: Int) {
3840
}
3941
}
4042
}
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+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ precommitTests = [
106106
"Array2D": run_Array2D,
107107
"ArrayAppend": run_ArrayAppend,
108108
"ArrayAppendReserved": run_ArrayAppendReserved,
109+
"ArrayAppendSequence": run_ArrayAppendSequence,
110+
"ArrayAppendArray": run_ArrayAppendArray,
109111
"ArrayInClass": run_ArrayInClass,
110112
"ArrayLiteral": run_ArrayLiteral,
111113
"ArrayOfGenericPOD": run_ArrayOfGenericPOD,

0 commit comments

Comments
 (0)