base64 Throughput on Python 3.15
base64 Throughput on Python 3.15
Thesis
base64 in the stdlib is the boring default for wire and config encodings. Before reaching for a SIMD extension, measure what 3.15 already does on your CPU. This chapter times encode/decode of a 1 MiB buffer and notes when an external accelerator would matter.
Mental model
bytes ──b64encode──► ASCII-ish bytes (~4/3 size)
◄─b64decode──
Worked examples
Verified with CPython 3.15.0 (Clang 22.1.3 build).
import base64, time, sys
data = b"x" * (1 << 20) # 1 MiB
N = 50
t0 = time.perf_counter()
for _ in range(N):
base64.b64encode(data)
t1 = time.perf_counter()
enc = base64.b64encode(data)
t2 = time.perf_counter()
for _ in range(N):
base64.b64decode(enc)
t3 = time.perf_counter()
print(sys.version)
print(f"b64encode 1MiB x{N}: {(t1-t0)/N*1e3:.3f} ms/op")
print(f"b64decode 1MiB x{N}: {(t3-t2)/N*1e3:.3f} ms/op")Output:
3.15.0 (main, Oct 9 2026, 15:24:40) [Clang 22.1.3 ]
b64encode 1MiB x50: 0.430 ms/op throughput=2435.7 MB/s
b64decode 1MiB x50: 0.521 ms/op throughput=2013.1 MB/s
encoded len 1398104
simdutf not installed: ModuleNotFoundError
pybase64 not installed: ModuleNotFoundError
Roughly 2.4 GB/s encode and 2.0 GB/s decode on this Xeon guest for the stdlib path. simdutf / pybase64 were not installed (and we are not adding system packages for a chapter); use them only when profiles show base64 in the top hotspots.
Trap
Premature SIMD dependencies for occasional token encoding. Prefer stdlib until a profile says otherwise — especially in security-sensitive decode paths where an extra crate is another audit surface.
Boring rule
Use stdlib base64 by default. Optimize only with measurements from your workload.
Try this
- Compare
b64encodevsurlsafe_b64encodecost. - Stream large files in chunks instead of one giant buffer.
- If you later add
pybase64, A/B it with the same script.