示例工程 24-performance-instruments

24-performance-instruments/Sources/Ch24Demo/App.swift

swift · 1.2 KB

import Ch24Kit
 
@main
struct Ch24Demo {
    static func main() {
        let values = Array(0..<1000)
        print("[24.1] loop = \(Algorithms.sumLoop(values))")
        print("[24.1] reduce = \(Algorithms.sumReduce(values))")
        print("[24.1] while = \(Algorithms.sumWhile(values))")
 
        let capacityWithReserve = Algorithms.buildArray(1000, reserve: true)
        let capacityWithoutReserve = Algorithms.buildArray(1000, reserve: false)
        print("[24.2] 预分配 capacity = \(capacityWithReserve)")
        print("[24.2] 未预分配 capacity = \(capacityWithoutReserve)")
 
        let measurement = Benchmark.measure(name: "demo", warmup: 1, rounds: 3) {
            Algorithms.sumLoop(values)
        }
        print("[24.3] 测量名字 = \(measurement.name)")
        print("[24.3] 中位数为正 = \(measurement.milliseconds > 0)")
        print("[24.3] checksum 非零 = \(measurement.checksum != 0)")
 
        let all = Benchmarks.run(iterations: 10_000)
        print("[24.4] 基准数量 = \(all.count),顺序 = \(all.map(\.name))")
 
        print("[24.5] MemoryLayout<Measurement> = \(MemoryLayout<Measurement>.size) 字节")
        print("[24.5] MemoryLayout<Duration> = \(MemoryLayout<Duration>.size) 字节")
    }
}