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# I'd love for this to work but it's not quite right right now. The table in the README is copied manually.
2
3import re
4
5def to_ns(val, unit):
6 v = int(val)
7 return v if unit == "ns" else v * 1000
8
9files = [
10 ("comparison-decoding", "Decoding"),
11 ("comparison-encoding", "Encoding")
12]
13
14for file in files:
15 with open(file[0] + ".md", "r") as f:
16 markdown = f.read()
17
18 # Split into sections by headings
19 sections = re.split(r"(?=^### )", markdown, flags=re.MULTILINE)
20
21 benchmarks = []
22 for section in sections:
23 header_match = re.match(r"### (.*?) metrics", section)
24 if not header_match:
25 continue
26 name = header_match.group(1).strip()
27
28 # Grab the unit (ns / µs / us) from the table header
29 unit_match = re.search(r"\((ns|μs|us)\)", section)
30 if not unit_match:
31 continue
32 unit = unit_match.group(1)
33
34 # Extract p50 columns from rows like: | swiftcbor | 123 | 456 | ...
35 row_pattern = r"\|\s*{}\s*\|\s*([0-9]+)\s*\|\s*([0-9]+)"
36 swift_match = re.search(row_pattern.format("swiftcbor"), section, re.IGNORECASE)
37 current_match = re.search(row_pattern.format("Current_run"), section, re.IGNORECASE)
38
39 if swift_match and current_match:
40 swift_p50 = to_ns(swift_match.group(2), unit)
41 current_p50 = to_ns(current_match.group(2), unit)
42 ratio = current_p50 / swift_p50 if swift_p50 > 0 else float("inf")
43 benchmarks.append((name, swift_p50, current_p50, ratio))
44
45 print("### " + file[1])
46 print("")
47 print("| Benchmark | SwiftCBOR (ns, p50) | CBOR (ns, p50) | % Improvement |")
48 print("|-----------|----------------|-----------|------------|")
49 for bench in benchmarks:
50 improvement = (bench[1] - bench[2]) / bench[1] * 100
51 print(f"| {bench[0]} | {bench[1]:,} | {bench[2]:,} | **{improvement:.2f}%** |")
52 print("")