Performance testing with measure blocks in Swift - Time & Space Complexity
Performance testing with measure blocks helps us see how long code takes to run as input grows.
We want to know how the running time changes when we test different input sizes.
Analyze the time complexity of the following code snippet.
func sumArray(_ numbers: [Int]) -> Int {
var total = 0
for number in numbers {
total += number
}
return total
}
measure {
_ = sumArray(Array(1...n))
}
This code sums all numbers in an array and measures how long it takes.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each number in the array to add it.
- How many times: Once for every item in the array (n times).
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 additions |
| 100 | About 100 additions |
| 1000 | About 1000 additions |
Pattern observation: The work grows directly with the number of items; double the items means double the work.
Time Complexity: O(n)
This means the time to run grows in a straight line with the input size.
[X] Wrong: "The measure block runs instantly no matter the input size."
[OK] Correct: The time depends on how many items we process; bigger inputs take longer.
Knowing how to measure and understand time growth helps you explain your code's speed clearly and confidently.
"What if we changed the sumArray function to sum only half the array? How would the time complexity change?"
Practice
measure blocks in Swift XCTest?Solution
Step 1: Understand the role of
measureblocksmeasureblocks are used to time how long code takes to run repeatedly.Step 2: Differentiate from other test purposes
Unlike checking correctness or compilation,measurefocuses on performance timing.Final Answer:
To measure how long a piece of code takes to run -> Option AQuick Check:
Performance timing = A [OK]
- Confusing performance test with correctness test
- Thinking measure checks compilation errors
- Assuming measure counts function calls
Solution
Step 1: Recall XCTest performance syntax
The correct syntax usesmeasure { }to wrap the code to be timed.Step 2: Identify incorrect options
Options B, C, and D are not valid XCTest methods for performance testing.Final Answer:
measure { /* code to test */ } -> Option DQuick Check:
Correct syntax = A [OK]
- Using outdated or incorrect method names
- Adding parentheses after measure incorrectly
- Confusing with other test function names
func testExample() {
measure {
let sum = (1...1000).reduce(0, +)
print(sum)
}
}Solution
Step 1: Understand the code inside measure
The code sums numbers 1 to 1000, which equals 500500, then prints it.Step 2: Know measure behavior
measure runs the block multiple times, so print outputs multiple times, plus it reports average time.Final Answer:
Prints 500500 multiple times and reports average time -> Option BQuick Check:
Repeated print of sum = C [OK]
- Thinking print runs only once
- Assuming measure blocks suppress output
- Expecting compilation error from print
func testPerformanceExample() {
measure {
let array = [1, 2, 3]
array.append(4)
}
}Solution
Step 1: Check array declaration
The array is declared withlet, making it immutable (constant).Step 2: Identify mutation attempt
Callingappend(4)tries to change the constant array, causing a compile error.Final Answer:
Cannot append to a constant array inside measure block -> Option CQuick Check:
Appending to let array = D [OK]
- Thinking measure forbids variable declarations
- Assuming array must be outside measure
- Ignoring let vs var immutability
measure?Solution
Step 1: Understand what to measure
We want to measure only sorting time, not array creation time.Step 2: Place array creation outside measure
Creating the array outside measure ensures creation time is excluded.Step 3: Sort inside measure block
Sorting inside measure captures only sorting performance.Final Answer:
Create the array outside measure, then sort inside measure -> Option AQuick Check:
Exclude setup, measure only sorting = B [OK]
- Including setup time inside measure
- Measuring nothing inside measure
- Sorting outside measure by mistake
Ready to prove it?
Pass the test and this pattern moves unknown
