Bird
Raised Fist0
Swiftprogramming~10 mins

Performance testing with measure blocks in Swift - Interactive Code Practice

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using measure blocks in Swift XCTest?
easy
A. To measure how long a piece of code takes to run
B. To check if the code compiles without errors
C. To test if the code produces the correct output
D. To count how many times a function is called

Solution

  1. Step 1: Understand the role of measure blocks

    measure blocks are used to time how long code takes to run repeatedly.
  2. Step 2: Differentiate from other test purposes

    Unlike checking correctness or compilation, measure focuses on performance timing.
  3. Final Answer:

    To measure how long a piece of code takes to run -> Option A
  4. Quick Check:

    Performance timing = A [OK]
Hint: Measure blocks check speed, not correctness or compilation [OK]
Common Mistakes:
  • Confusing performance test with correctness test
  • Thinking measure checks compilation errors
  • Assuming measure counts function calls
2. Which of the following is the correct syntax to start a performance test block in Swift XCTest?
easy
A. testPerformance { /* code to test */ }
B. measureBlock { /* code to test */ }
C. performanceTest { /* code to test */ }
D. measure { /* code to test */ }

Solution

  1. Step 1: Recall XCTest performance syntax

    The correct syntax uses measure { } to wrap the code to be timed.
  2. Step 2: Identify incorrect options

    Options B, C, and D are not valid XCTest methods for performance testing.
  3. Final Answer:

    measure { /* code to test */ } -> Option D
  4. Quick Check:

    Correct syntax = A [OK]
Hint: Use exactly 'measure { }' to start performance tests [OK]
Common Mistakes:
  • Using outdated or incorrect method names
  • Adding parentheses after measure incorrectly
  • Confusing with other test function names
3. What will be the output when running this Swift XCTest performance test?
func testExample() {
    measure {
        let sum = (1...1000).reduce(0, +)
        print(sum)
    }
}
medium
A. Prints nothing and reports average time
B. Prints 500500 multiple times and reports average time
C. Prints 1000 once and reports average time
D. Compilation error due to print inside measure

Solution

  1. Step 1: Understand the code inside measure

    The code sums numbers 1 to 1000, which equals 500500, then prints it.
  2. Step 2: Know measure behavior

    measure runs the block multiple times, so print outputs multiple times, plus it reports average time.
  3. Final Answer:

    Prints 500500 multiple times and reports average time -> Option B
  4. Quick Check:

    Repeated print of sum = C [OK]
Hint: measure runs code repeatedly, so prints repeat [OK]
Common Mistakes:
  • Thinking print runs only once
  • Assuming measure blocks suppress output
  • Expecting compilation error from print
4. Identify the error in this Swift XCTest performance test code:
func testPerformanceExample() {
    measure {
        let array = [1, 2, 3]
        array.append(4)
    }
}
medium
A. No error; code runs fine
B. measure block must not contain variable declarations
C. Cannot append to a constant array inside measure block
D. Array must be declared outside measure block

Solution

  1. Step 1: Check array declaration

    The array is declared with let, making it immutable (constant).
  2. Step 2: Identify mutation attempt

    Calling append(4) tries to change the constant array, causing a compile error.
  3. Final Answer:

    Cannot append to a constant array inside measure block -> Option C
  4. Quick Check:

    Appending to let array = D [OK]
Hint: Use var for arrays you want to change inside measure [OK]
Common Mistakes:
  • Thinking measure forbids variable declarations
  • Assuming array must be outside measure
  • Ignoring let vs var immutability
5. You want to measure the performance of sorting a large array but avoid including array creation time in the measurement. Which is the best way to write the test using measure?
hard
A. Create the array outside measure, then sort inside measure
B. Create the array inside the measure block, then sort it
C. Create and sort the array outside measure, then print inside measure
D. Create the array inside measure, but sort outside measure

Solution

  1. Step 1: Understand what to measure

    We want to measure only sorting time, not array creation time.
  2. Step 2: Place array creation outside measure

    Creating the array outside measure ensures creation time is excluded.
  3. Step 3: Sort inside measure block

    Sorting inside measure captures only sorting performance.
  4. Final Answer:

    Create the array outside measure, then sort inside measure -> Option A
  5. Quick Check:

    Exclude setup, measure only sorting = B [OK]
Hint: Put setup outside measure, test only target code inside [OK]
Common Mistakes:
  • Including setup time inside measure
  • Measuring nothing inside measure
  • Sorting outside measure by mistake
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard