forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_unicodedata_category.py
More file actions
78 lines (60 loc) · 1.81 KB
/
benchmark_unicodedata_category.py
File metadata and controls
78 lines (60 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
"""Benchmark Python-level unicodedata.category() lookups.
Runs three fixed workloads:
- all Unicode code points
- BMP only
- ASCII only
"""
from __future__ import annotations
import statistics
import time
import unicodedata
LOOPS = 5
SAMPLES = 7
DATASETS = {
"all": "".join(map(chr, range(0x110000))),
"bmp": "".join(map(chr, range(0x10000))),
"ascii": "".join(map(chr, range(0x80))),
}
def run_once(chars: str) -> tuple[float, int]:
category = unicodedata.category
checksum = 0
t0 = time.perf_counter()
for _ in range(LOOPS):
for ch in chars:
gc = category(ch)
checksum += ord(gc[0]) + ord(gc[1])
elapsed = time.perf_counter() - t0
return elapsed, checksum
def benchmark(name: str, chars: str) -> None:
lookups = len(chars) * LOOPS
# Warm up specialization and caches before timing.
run_once(chars)
samples = []
checksum = None
for _ in range(SAMPLES):
elapsed, checksum = run_once(chars)
samples.append(elapsed)
best = min(samples)
median = statistics.median(samples)
mean = statistics.fmean(samples)
print(f"dataset: {name}")
print(f"codepoints: {len(chars)}")
print(f"lookups/sample: {lookups}")
print(f"checksum: {checksum}")
print(f"best_s: {best:.6f}")
print(f"median_s: {median:.6f}")
print(f"mean_s: {mean:.6f}")
print(f"best_ns_per_lookup: {best * 1e9 / lookups:.2f}")
print(f"median_ns_per_lookup: {median * 1e9 / lookups:.2f}")
print()
def main() -> None:
print(f"python: {unicodedata.unidata_version=}")
print(f"samples: {SAMPLES}")
print(f"loops: {LOOPS}")
print()
benchmark("all", DATASETS["all"])
benchmark("bmp", DATASETS["bmp"])
benchmark("ascii", DATASETS["ascii"])
if __name__ == "__main__":
main()