Skip to content

Code Benchmarking

mattreaganmozilla edited this page May 2, 2025 · 3 revisions

Benchmarking Utilities

This page contains information on internal utilities that can be leveraged to help with performance measurements.

BenchmarkSteps.swift

This tool provides a basic benchmarking utility to measure code that occurs in sequential steps. This can be helpful for breaking down the performance runtime and identifying how particular sections of code contribute to the overall time profile.

Example usage

let benchmark = BenchmarkSteps()

Thread.sleep(forTimeInterval: 0.5)   // Perform some work
benchmark.step("Part 1")

Thread.sleep(forTimeInterval: 0.25)   // More work
benchmark.step("Part 2")

Thread.sleep(forTimeInterval: 1.0)    // Even more work
benchmark.step("Part 3")

benchmark.finish()

Output

[Benchmark Completed]
	[Step: 'Part 1'] 501 ms
	[Step: 'Part 2'] 251 ms
	[Step: 'Part 3'] 1000 ms
[End] Total: 1752 ms
Clone this wiki locally