0
0
Swiftprogramming~10 mins

Performance testing with measure blocks in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Arun
Btest
Cexecute
Dmeasure
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'run' or 'execute' instead of 'measure'.
Forgetting to call the measure method on self.
2fill in blank
medium

Complete 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'
Aorder
Bsorted
Csort
Darrange
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'.
3fill in blank
hard

Fix 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'
Ainsert
Badd
Cappend
Dpush
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'add' or 'push' which are not valid Array methods in Swift.
Using 'insert' without specifying an index.
4fill in blank
hard

Fill 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'
Areversed
Bsorted
Cmap
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sorted()' which sorts instead of reversing.
Using 'map' or 'filter' which do not reverse arrays.
5fill in blank
hard

Fill 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'
Afilter
B%
Ccount
Dmap
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.