SwiftExecutionTimer

main

The SwiftExecutionTimer package allows you to measure the execution times of specific code segments.
MichK1/SwiftExecutionTimer

SwiftExecutionTimer

The SwiftExecutionTimer package allows you to measure the execution times of specific code segments.

Installation

SwiftExecutionTimer can be installed using Swift Package Manager:

dependencies: [
        .package(url: "https://github.com/MichK1/SwiftExecutionTimer.git", exact: "0.1.0"),
    ]

Usage

The ExecutionTimer class helps measure execution times by adding marks at various points during code execution. Each mark can optionally include a label for clarity. To add a mark, call the ExecutionTimer.mark() method.

Features:

  • Time Points: Marks are stored as an array of TimePoint objects, recorded relative to the first mark (set to 0).
  • Durations: The time spans between consecutive marks are represented by the Duration structure, which includes:
    • duration: The elapsed time.
    • startLabel and endLabel: Labels from the corresponding time points.

Time Sources

ExecutionTimer supports two time sources:

  • .monotonic: Based on system uptime in seconds.
  • .cpuUtilizationTime: Based on CPU time spent executing the current process (user mode only). More details can be found here.

Example: Measuring Sorting Time

The example below measures the time taken to sort an array of 1000 random Double values, repeated 100 times:

import SwiftExecutionTimer

func runAndMeasure() {
    let executionTimer = ExecutionTimer() // Initialize the timer with the default `.monotonic` source
    
    (0..<100).forEach { trialIndex in
        executionTimer.mark("ARRAY INIT")      // Mark the beginning of array initialization.
        var array = (0..<1000).map { _ in Double.random(in: 0...1) }
        
        executionTimer.mark("SORT BEGIN")      // Mark the beginning of array sorting.
        array.sort()                           // Sort the array
        executionTimer.mark("SORT END")        // Mark the end of array sorting.
    }
    
    // Calculate durations for time intervals starting at "SORT BEGIN".
    let durations = executionTimer.durations(filter: .startLabel(.exact("SORT BEGIN")))
    let minTime = durations.min(by: { $0.duration < $1.duration })?.duration
    let maxTime = durations.max(by: { $0.duration < $1.duration })?.duration
    let sortingTime = executionTimer.sumDurations(filter: .startLabel(.exact("SORT BEGIN")))
    
    // Print results
    print("Sorting 100 arrays with 1000 Doubles took: \(sortingTime) seconds")
    print("Minimum sorting time: \(minTime ?? 0) seconds")
    print("Maximum sorting time: \(maxTime ?? 0) seconds")
}

Further examples

Additional example, sorting execution time comparisons, can be found in the SortMeasure example.

License

SwiftExecutionTimer is available under the MIT license. See the LICENSE file for more informations.

Description

  • Swift Tools 5.8.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sat May 24 2025 01:46:34 GMT-0900 (Hawaii-Aleutian Daylight Time)