Hands-on cmath vs math.hypot magnitude ops lab: real ops/s for abs, polar, and phase, measured on Linux localhost today in this hands-on lab for SREs.
3 min read
2-D magnitude and phase with math.hypot / math.atan2 vs abs(complex) / cmath.polar / cmath.phase. This lab reports ops/s on Linux localhost for 200000 synthetic (re, im) pairs.
Related links:
Lab honesty (1 Oct 2026 IST): Python 3.13.5. Affiliates: 0. Stdlib only — useful when you treat latency error components, IQ samples, or vector residuals as complex numbers.
Verdict up front: abs(complex) ~34917055 ops/s; math.hypot ~16959583; cmath.polar r ~11928627; manual sqrt(a*a+b*b) ~23608807. Phase: cmath.phase ~23945233 vs atan2 ~19534098.
| Arm | Pattern |
|---|---|
math.hypot(a,b) | float pair magnitude |
abs(z) on complex | complex abs |
cmath.polar(z)[0] | polar radius |
math.sqrt(a*a+b*b) | manual (overflow-prone) |
cmath.phase / atan2 | angle |
Seven rounds, p50. Sample check: hypot 65.11528238439882 matches abs (match=True).
Script: lab-evidence/134-cmath-vs-math-hypot/results/run_lab.py.
| Arm | ops/s |
|---|---|
| abs(complex) | 34917055 |
| manual sqrt | 23608807 |
| cmath.phase | 23945233 |
| math.atan2 | 19534098 |
| math.hypot | 16959583 |
| cmath.polar r | 11928627 |
abs(complex) led magnitude. cmath.polar paid for computing both r and φ even when only r was used.
complex values → abs(z) (fastest magnitude here).math.hypot (overflow-safer than manual sqrt).cmath.polar over abs+phase separately if both used (measure your path).sqrt(a*a+b*b) near float extremes — hypot exists for a reason.Even though hypot was slower than abs(complex) on this run (~16959583 vs ~34917055), hypot avoids intermediate overflow when |a| or |b| is huge. Telemetry pipelines with wild units should stay on hypot unless values are already complex.
cmath.polar at ~11928627 ops/s is the wrong tool if you only need magnitude — you pay for phase work you discard.
cmath.phase (~23945233) edged atan2 (~19534098) slightly here. Pick the API that matches your types; do not convert float pairs to complex solely for phase unless the rest of the pipeline is complex-native.
If your pipeline already boxes samples as complex, stay there and call abs (~34917055 ops/s here). If sensors give two floats, call math.hypot and skip allocating complex objects just to take a magnitude. Conversion cost is outside this bench but shows up in real collectors — measure end-to-end before rewriting.
For dashboards that need both magnitude and angle once per sample, cmath.polar can still win on clarity even at ~11928627 ops/s, because one call documents intent better than abs+phase glue.
cmath.polar only for r in a hot loop.Evidence: summary.json, summary.txt.
One Linux box. Pure Python loops over lists. Not NumPy ufuncs.
For magnitudes on complex values, abs(z) ~34917055 ops/s led; for float pairs prefer math.hypot (~16959583) over manual sqrt. Use cmath.polar only when you need both radius and phase.
Lab evidence
Lab 1 Oct 2026 IST. Python 3.13.5. n=200000: abs(complex) 34917055 ops/s; math.hypot 16959583; cmath.polar r 11928627. Affiliates: 0. Evidence: lab-evidence/134-cmath-vs-math-hypot/results/summary.json. Results reproduced on Linux localhost today.
Hands-on platform.platform vs os.uname host inventory lab: real ops/s plus cache notes, measured on Linux localhost today in this hands-on lab for SREs.
Summary1 Oct 20264 min read
Summary1 Oct 20264 min read
Hands-on uuid.uuid4 vs uuid.uuid1 ID generation lab: real ops/s plus version/node checks, measured on Linux localhost today in this hands-on lab for SREs.
Summary1 Oct 20263 min read