A fast, safe, and efficient CBOR serialization library for Swift on any platform.
swiftpackageindex.com/thecoolwinter/CBOR/1.1.1/documentation/cbor
atproto
swift
cbor
1//
2// Profiling.swift
3// CBOR
4//
5// Created by Khan Winter on 10/20/25.
6//
7
8import CBOR
9import Foundation
10
11struct Profiling {
12 @_optimize(none)
13 func blackhole(_ val: some Any) { }
14
15 func time(_ cbor: () throws -> Void, _ json: () throws -> Void) throws {
16 guard #available(macOS 15.0, *) else { fatalError() }
17 func calculateStats(_ measurements: [Duration]) -> (average: Double, stddev: Double) {
18 let values = measurements.map { Double($0.attoseconds) / 1e15 }
19 let avg = values.reduce(0, +) / Double(values.count)
20
21 let variance = values.map { pow($0 - avg, 2) }.reduce(0, +) / Double(values.count)
22 let stddev = sqrt(variance)
23
24 return (avg, stddev)
25 }
26
27 let iterations = 10
28
29 var cborTimes: [Duration] = []
30 var jsonTimes: [Duration] = []
31
32 for _ in 0..<iterations {
33 let cborTime = try SuspendingClock().measure {
34 try cbor()
35 }
36 cborTimes.append(cborTime)
37
38 let jsonTime = try SuspendingClock().measure {
39 try json()
40 }
41 jsonTimes.append(jsonTime)
42 }
43
44 let cborStats = calculateStats(cborTimes)
45 let jsonStats = calculateStats(jsonTimes)
46
47 let percentChange = 100.0 * ((cborStats.average - jsonStats.average) / jsonStats.average)
48
49 print("CBOR: \(String(format: "%.3f", cborStats.average)) ± \(String(format: "%.3f", cborStats.stddev)) ms")
50 print("JSON: \(String(format: "%.3f", jsonStats.average)) ± \(String(format: "%.3f", jsonStats.stddev)) ms")
51 print("Difference: \(percentChange > 0 ? "+" : "")\(String(format: "%.2f", percentChange))%")
52 }
53
54 func complex() throws {
55 try time {
56 for _ in 0..<1000 {
57 blackhole(try CBOREncoder().encode(Company.mock))
58 }
59 } _: {
60 for _ in 0..<1000 {
61 blackhole(try JSONEncoder().encode(Company.mock))
62 }
63 }
64
65 }
66
67 func dictionary() throws {
68 let data: [String: Int] = (0..<1000).reduce(into: [String: Int](), { $0[String(describing: $1)] = $1 })
69
70 try time {
71 for _ in 0..<100 {
72 blackhole(try CBOREncoder().encode(data))
73 }
74 } _: {
75 for _ in 0..<100 {
76 blackhole(try JSONEncoder().encode(data))
77 }
78 }
79
80 }
81
82 func int() throws {
83 try time {
84 for _ in 0..<1000 {
85 blackhole(try CBOREncoder().encode(Int.random(in: Int.min..<Int.max)))
86 }
87 } _: {
88 for _ in 0..<1000 {
89 blackhole(try JSONEncoder().encode(Int.random(in: Int.min..<Int.max)))
90 }
91 }
92
93 }
94
95 func string() throws {
96 try time {
97 for _ in 0..<1000 {
98 blackhole(try CBOREncoder().encode("ABCDEFGHIJKLMNOP"))
99 }
100 } _: {
101 for _ in 0..<1000 {
102 blackhole(try JSONEncoder().encode("ABCDEFGHIJKLMNOP"))
103 }
104 }
105
106 }
107
108 func data() throws {
109 try time {
110 for _ in 0..<1000 {
111 blackhole(try CBOREncoder().encode(Data([0x01, 0x02, 0x03, 0x04])))
112 }
113 } _: {
114 for _ in 0..<1000 {
115 blackhole(try JSONEncoder().encode(Data([0x01, 0x02, 0x03, 0x04])))
116 }
117 }
118
119 }
120}