Hands-on argparse vs manual sys.argv parse lab: real parses/s for small CLI flag sets on fixed argv, measured on Linux localhost in this lab for SREs.
4 min read
Parse a small CLI: argparse.ArgumentParser.parse_args vs a manual sys.argv walk. This lab reports parses/s on fixed argv lists on Linux localhost.
Related links:
Lab honesty (1 Oct 2026 IST): Python 3.13.5. Affiliates: 0. No Docker. Flags: -v/--verbose, -n/--count, -o/--output, optional path.
Verdict up front (flags argv): manual ~1539197 parses/s vs argparse reuse ~77040 (~19.98×); new parser each call ~6873. Prefer argparse for real CLIs; manual only for tiny hot wrappers.
| Arm | Pattern |
|---|---|
| argparse reuse | one ArgumentParser, many parse_args |
| argparse new each time | build parser inside loop |
| manual walk | lightweight flag scanner |
Script: lab-evidence/97-argparse-vs-sys-argv/results/run_lab.py.
| Variant | argparse reuse | argparse new each | manual |
|---|---|---|---|
| minimal | 165472 | 7802 | 6475174 |
flags (-v -n 3 -o …) | 77040 | 6873 | 1539197 |
| long flags | 78321 | 6823 | 1585153 |
format_help: ~9324/s (help text generation cost).
int(), conflict checks, and usage formatting.--help, validation, and subcommands.Internal wrappers with two booleans and no public help text can scan sys.argv — keep them tiny and tested. The moment you add mutual exclusion, typed options, or user-facing errors, switch to argparse.
Real tools pay import time, logging setup, and config load far above one parse_args. This lab isolates the parser so the ~20.0× gap is visible — it is not a mandate to abandon argparse in production CLIs.
format_help ran at ~9324/s on this schema — cheap at human scale. Shipping a hand parser without --help saves nanoseconds and costs support time. Count that as argparse’s real product.
This schema is flat flags only. Subparsers, mutually exclusive groups, and custom types widen the argparse vs manual gap further — manual clones become bug farms. That is another reason the microbench win for sys.argv should not drive public CLI design.
argparse applied type=int and defaults in one call. The manual arm trusted int() on the next token — fine in a lab, brittle if users pass --count without a value. Validation belongs in the parser unless the wrapper is truly private.
ArgumentParser inside a hot loop.Evidence: summary.json, summary.txt.
One Linux box. One small flag schema. Not click/typer/fire. Not GNU getopt parity.
A normal CLI parses argv once at startup. Even at argparse’s ~77k parses/s, a single parse is tens of microseconds — invisible next to importing libraries. Chase clarity first; chase parses/s only inside unusual hot loops (generated tools, fuzz harnesses).
On a flags argv, manual ~1539197 parses/s vs argparse ~77040. Default to argparse for CLIs; keep manual sys.argv for tiny internal wrappers — and always reuse the parser object if you parse repeatedly.
Lab evidence
Lab 1 Oct 2026 IST. Python 3.13.5. flags argv: manual 1539197/s vs argparse reuse 77040/s (~19.98x); new parser each 6873/s. Affiliates: 0. Evidence: lab-evidence/97-argparse-vs-sys-argv/. Reused parser in the loop.
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
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
Hands-on signal SIGUSR1 vs threading.Event wakeup lab: real p50 latency in microseconds, measured on Linux localhost today in this hands-on lab for SREs.
Summary1 Oct 20264 min read