Skip to content

Add simple benchmark for append(contentsOf:) #5436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions benchmark/single-source/ArrayAppend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import TestsUtils

// Append single element
@inline(never)
public func run_ArrayAppend(_ N: Int) {
for _ in 0..<N {
Expand All @@ -26,6 +27,7 @@ public func run_ArrayAppend(_ N: Int) {
}
}

// Append single element with reserve
@inline(never)
public func run_ArrayAppendReserved(_ N: Int) {
for _ in 0..<N {
Expand All @@ -38,3 +40,36 @@ public func run_ArrayAppendReserved(_ N: Int) {
}
}
}

// Append a sequence. Length of sequence unknown so
// can't pre-reserve capacity. Should be comparable
// to append single elements.
@inline(never)
public func run_ArrayAppendSequence(_ N: Int) {
let seq = stride(from: 0, to: 10_000, by: 1)
for _ in 0..<N {
for _ in 0..<10 {
var nums = [Int]()
for _ in 0..<4 {
nums.append(contentsOf: seq)
}
}
}
}

// Append another array. Length of sequence known so
// can pre-reserve capacity.
@inline(never)
public func run_ArrayAppendArray(_ N: Int) {
let other = Array(repeating: 1, count: 10_000)
for _ in 0..<N {
for _ in 0..<10 {
var nums = [Int]()
for _ in 0..<4 {
// note, this uses += rather than append(contentsOf:),
// to ensure operator doesn't introduce inefficiency
nums += other
}
}
}
}
2 changes: 2 additions & 0 deletions benchmark/utils/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ precommitTests = [
"Array2D": run_Array2D,
"ArrayAppend": run_ArrayAppend,
"ArrayAppendReserved": run_ArrayAppendReserved,
"ArrayAppendSequence": run_ArrayAppendSequence,
"ArrayAppendArray": run_ArrayAppendArray,
"ArrayInClass": run_ArrayInClass,
"ArrayLiteral": run_ArrayLiteral,
"ArrayOfGenericPOD": run_ArrayOfGenericPOD,
Expand Down