Skip to content

gh-131196: Improve uuid.__str__() performance a little more#140663

Open
akx wants to merge 1 commit intopython:mainfrom
akx:faster-uuid-str
Open

gh-131196: Improve uuid.__str__() performance a little more#140663
akx wants to merge 1 commit intopython:mainfrom
akx:faster-uuid-str

Conversation

@akx
Copy link
Copy Markdown
Contributor

@akx akx commented Oct 27, 2025

This PR follows up on the PR #131197 to further slightly (~2%) improve the performance of UUID.__str__() by avoiding trampolining through the hex and bytes properties.

(Given that stringifying UUIDs is likely a pretty common use case, maybe this would be worth a speedup alternative in _uuidmodule.c?)

Here's what a pytest-benchmark comparison says (suuid being a copy of this uuid.py and suuid_orig the one from 3416e7c).

Name (time in ns) Min Max Mean StdDev Median IQR Outliers OPS (Mops/s) Rounds Iterations
test_suuid[suuid] 370.0099 (1.0) 442.6828 (1.05) 401.2448 (1.0) 20.4759 (2.85) 398.8881 (1.0) 20.2299 (1.88) 3;1 2.4922 (1.0) 10 1,355,472
test_suuid[suuid_orig] 402.7837 (1.09) 422.9409 (1.0) 410.2969 (1.02) 7.1930 (1.0) 408.5021 (1.02) 10.7775 (1.0) 4;0 2.4373 (0.98) 10 1,245,293
Test code ```python import suuid_orig import suuid import pytest

@pytest.mark.parametrize("module", [suuid, suuid_orig])
def test_suuid(benchmark, module):
u = module.uuid4()
def b():
assert str(u)
benchmark(b)

</details>


<!-- gh-issue-number: gh-131196 -->
* Issue: gh-131196
<!-- /gh-issue-number -->

@yihong0618
Copy link
Copy Markdown
Contributor

FYI: seems there's already one Pull request for uuid
#139123

@akx
Copy link
Copy Markdown
Contributor Author

akx commented Oct 27, 2025

@yihong0618 Ah, neat! That'd cover my other UUID PR #140666 among other things :)

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 1, 2026

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants