A lightweight Swift library that allows you to measure code execution time using labeled intervals.
SwiftExecutionTimer can be installed using Swift Package Manager:
dependencies: [
.package(url: "https://github.com/MichK1/SwiftExecutionTimer.git", from: "0.2.0"),
]
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. You can add a mark by calling the method ExecutionTimer.mark(), optionally providing a label.
- Time Points: Marks are stored as an array of
TimePointobjects, recorded relative to the first mark (set to0). - Durations: The time spans between consecutive marks are represented by the
Durationstructure, which includes:duration: The elapsed time.startLabelandendLabel: Labels from the corresponding time points.
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 are available here.
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")
}You can find more examples in the Examples folder. You can see them in action by running swift products:
- SimpleDownload
- SimpleMeasure
- SortMeasure
% swift run SimpleDownload
% swift run SimpleMeasure
% swift run SortMeasureSwiftExecutionTimer is available under the MIT license. See the LICENSE file for more information.