Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start a performance test using measure block.
Swift
func testPerformanceExample() {
self.[1] {
// Code to measure
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'run' or 'execute' instead of 'measure'.
Forgetting to call the measure method on self.
✗ Incorrect
The measure block is used in XCTest to measure performance of the code inside it.
2fill in blank
mediumComplete the code to measure the performance of sorting an array.
Swift
func testSortPerformance() {
let array = [5, 3, 1, 4, 2]
self.measure {
let _ = array.[1]()
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sort()' which sorts in place and returns void, causing errors.
Using non-existent methods like 'order' or 'arrange'.
✗ Incorrect
The 'sorted()' method returns a sorted array without modifying the original, suitable for performance testing.
3fill in blank
hardFix the error in the measure block to correctly measure the performance of appending to an array.
Swift
func testAppendPerformance() {
var array = [Int]()
self.measure {
array.[1](1)
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'add' or 'push' which are not valid Array methods in Swift.
Using 'insert' without specifying an index.
✗ Incorrect
The correct method to add an element to the end of an array in Swift is 'append'.
4fill in blank
hardFill both blanks to measure the performance of reversing an array and assigning it to a new variable.
Swift
func testReversePerformance() {
let array = [1, 2, 3, 4, 5]
self.measure {
let reversed = array.[1]()
_ = reversed.[2]()
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sorted()' which sorts instead of reversing.
Using 'map' or 'filter' which do not reverse arrays.
✗ Incorrect
The 'reversed()' method returns a reversed collection. Calling it twice is valid but redundant here.
5fill in blank
hardFill all three blanks to measure the performance of filtering even numbers from an array and counting them.
Swift
func testFilterCountPerformance() {
let numbers = [1, 2, 3, 4, 5, 6]
self.measure {
let evens = numbers.[1] { $0 [2] 2 == 0 }
let count = evens.[3]
_ = count
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'map' instead of 'filter' to select elements.
Using '==' instead of '%' to check even numbers.
Using 'count()' instead of the 'count' property.
✗ Incorrect
Use 'filter' to select even numbers, '%' to check divisibility, and 'count' to count them.