Plate 75
uuid.uuid4 vs uuid.uuid1: Localhost Lab
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.
Aditya Challa3 min read
Intro — what this post promises
Generate IDs with uuid.uuid4 (random) vs uuid.uuid1 (time + node). This lab reports ops/s on Linux localhost, plus version/node checks.
Related links:
- path read text vs open localhost lab
- intenum vs int localhost lab
- dataclass asdict vs vars localhost lab
- tracemalloc snapshot localhost lab
- selectors vs select localhost lab
- literal eval vs json localhost lab
- cmath vs math hypot localhost lab
- signal vs event wakeup localhost lab
Lab honesty (1 Oct 2026 IST): Python 3.13.5. Affiliates: 0. Differentiates from uuid4-vs-secrets (lab 41) — that post compared uuid4 to secrets tokens; this one is uuid4 vs uuid1.
Verdict up front (n=100000): uuid4 ~966847 ops/s; uuid1 ~217847; with str(): uuid4 ~598452 vs uuid1 ~193085.
Arms
| Arm | Pattern |
|---|---|
uuid.uuid4() | random v4 |
uuid.uuid1() | time/node v1 |
str(uuid.*) | hyphenated string |
.hex | 32-char hex |
Seven rounds, p50. Sample versions: v4=4, v1=1, uuid1 node present=True.
Lab topology
Script: lab-evidence/138-uuid4-vs-uuid1/results/run_lab.py.
Lead table (p50 ops/s)
| Arm | ops/s |
|---|---|
| uuid4 | 966847 |
| uuid4.hex | 758980 |
| uuid4 str | 598452 |
| uuid1 | 217847 |
| uuid1.hex | 215276 |
| uuid1 str | 193085 |
uuid4 led by about 4.4× on object generation.
Semantics (why not only speed)
- uuid4: random; no MAC/time leakage; preferred for public IDs.
- uuid1: embeds timestamp + node; sortable-ish by time; can leak host identity.
Sample uuid1: f0ae2ffa-bd3a-11f1-b102-822b383dbcd0. Sample uuid4: 5a0e02bf-cad4-45ee-bdc2-acda319eb37c.
Reading it for SRE work
- Request IDs / public tokens → uuid4 (and see lab 41 if comparing to
secrets). - Need rough time ordering without a DB sequence → uuid1 can help; know the privacy trade.
- Stringifying costs real throughput — cache
stronly if you emit often. - Do not assume uniqueness measurements here — this lab is throughput + version checks only.
Stringify tax
str(uuid4) fell to ~598452 ops/s from ~966847 object-only. Same pattern for uuid1 (~193085). Prefer .hex when you want compact text without hyphens (~758980).
Privacy vs ordering
If logs must not reveal which physical host minted an ID, skip uuid1 — its node field was non-zero on this box (node=True). If you only need “roughly increasing” IDs for debugging, uuid1’s timestamp can help operators skim timelines, but a server-side monotonic ID is usually clearer. Measure your generator under load before blaming the database for insert latency.
Ops vs secrets (pointer)
Lab 41 compared uuid4 to secrets token helpers. This post’s peer is uuid1. If you need opaque URL-safe tokens rather than UUID layout, prefer the secrets lab’s guidance; if you need UUID-shaped IDs, uuid4 ~966847 ops/s is the faster stdlib UUID here versus uuid1’s ~217847.
Pitfalls
- Using uuid1 in multi-tenant SaaS where node/MAC leakage matters.
- Comparing to lab 41 secrets numbers without stating the peer.
- Expecting uuid1 to be “free” because it is not CSPRNG — it was slower here.
- Treating throughput as a uniqueness proof.
Reproduce
Evidence: summary.json, summary.txt.
Limits
One Linux box. Generation only — not collision tests, not DB index locality studies.
Store the UUID form your APIs already emit — converting layouts in every handler wastes the generator win.
Takeaway
uuid4 ~966847 ops/s beat uuid1 ~217847 and avoids time/node leakage. Prefer uuid4 for general IDs; use uuid1 only when you explicitly want its time+node properties.
Lab evidence
What I found running this
Lab 1 Oct 2026 IST. Python 3.13.5 on Linux localhost. n=100000, seven rounds, p50: uuid4 966847 ops/s; uuid1 217847. Checked version 4 vs 1 and uuid1 node present=True. Not lab 41 secrets. Affiliates: 0. Evidence: lab-evidence/138-uuid4-vs-uuid1/.
Related links
Plate 17
platform vs os.uname Inventory: Localhost Lab
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.
1 Oct 2026
Plate 76
cmath vs math.hypot Magnitudes: Localhost Lab
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.
1 Oct 2026
Plate 09
dataclasses.asdict vs vars: Localhost Lab
1 Oct 2026