3 min read
Tokenize CLI-like strings with shlex.split vs str.split. This lab reports ops/s on Linux localhost, plus a quoted-argument correctness check.
Related links:
Lab honesty (1 Oct 2026 IST): Python 3.13.5. Affiliates: 0. Not a tempfile remake — NamedTemporaryFile vs Spooled is already covered in lab 87; perf_counter vs time is lab 57.
Verdict up front (n=50000): simple line — str.split ~8888134 ops/s vs shlex ~64955; quoted line — split ~6389123 vs shlex ~54837.
| Arm | Pattern |
|---|---|
| shlex / split on simple flags | no quotes |
| shlex / split on quoted values | spaces inside quotes |
| posix-like path flags | = forms |
Seven rounds, p50.
Script: lab-evidence/140-shlex-vs-split/results/run_lab.py.
| Arm | ops/s |
|---|---|
| str.split simple | 8888134 |
| str.split quoted | 6389123 |
| str.split posix-like | 10936908 |
| shlex simple | 64955 |
| shlex posix-like | 64962 |
| shlex quoted | 54837 |
str.split won throughput by roughly 136.8× on the simple line — and got the quoted line wrong.
shlex tokens: ['deploy', '--env', 'prod west', '--cmd', 'echo hello world', '--tag', 'v1.2.3']
str.split tokens: ['deploy', '--env', '"prod', 'west"', '--cmd', '"echo', 'hello', 'world"', '--tag', 'v1.2.3']
shlex kept prod west as one token (preserves_quoted_space=True). str.split shattered quotes into junk fragments — fatal for deploy CLIs and runbook parsers.
shlex.split (or argparse).str.split is fine and fast.shell=True when you can.Paying ~54837 ops/s instead of ~6389123 is cheap next to a broken --env value. Tokenizing config once at process start makes the gap irrelevant.
If you must parse millions of simple metric tags with no quotes, split wins; document the no-quote invariant in the schema.
Internal metric tag lists and whitespace-separated host files with a documented “no quotes” rule can stay on str.split at ~8888134 ops/s. Put that invariant next to the parser in code review checklists. The moment a human can type a path with spaces, graduate to shlex — the ~64955 ops/s cost will not show up beside network I/O.
Agent frameworks that accept free-form “run this command” strings should default to shlex (or refuse shell metacharacters) rather than inventing another splitter.
Evidence: summary.json, summary.txt.
One Linux box. POSIX shlex defaults. Not a full shell grammar.
Keep a golden fixture with nested quotes in CI so a future “performance cleanup” cannot regress parsing.
str.split ~8888134 ops/s crushed shlex.split ~64955 on simple text, but failed quoted args. Use shlex whenever quotes matter; use split only for quote-free, trusted tokens.
Lab evidence
Ran the packaged localhost lab on Linux with Python 3.13.5. Seven rounds, p50: str.split reached 8,888,134 ops/s on simple input while shlex reached 64,955; quoted input preserved spaces only with shlex. Affiliates: 0.