summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGrigory Bukovsky <32143244+booqoffsky@users.noreply.github.com>2025-03-13 22:57:38 (GMT)
committerGitHub <noreply@github.com>2025-03-13 22:57:38 (GMT)
commit1121c80fdad1fc1a175f4691f33272cf28a66e83 (patch)
tree7c4db6e8f582c31cdbd34278d000dfd9939ebb3c
parentd7d22899e2fdfdc707f98d7297d9406de91b7e0d (diff)
downloadcpython-1121c80fdad1fc1a175f4691f33272cf28a66e83.zip
cpython-1121c80fdad1fc1a175f4691f33272cf28a66e83.tar.gz
cpython-1121c80fdad1fc1a175f4691f33272cf28a66e83.tar.bz2
gh-131196: Improve perfomance of `UUID.hex` and `UUID.__str__` by ~10% (#131197)
Results before and after the fix: ``` hex before: 0.021755493999989994 after: 0.01465080400066654 str before: 0.06381790500017814 after: 0.05134949700004654 ``` Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
-rw-r--r--Lib/uuid.py7
-rw-r--r--Misc/NEWS.d/next/Library/2025-03-13-19-53-57.gh-issue-131196.3sBFv2.rst1
2 files changed, 4 insertions, 4 deletions
diff --git a/Lib/uuid.py b/Lib/uuid.py
index 7a12b48..6f7a7a3 100644
--- a/Lib/uuid.py
+++ b/Lib/uuid.py
@@ -321,9 +321,8 @@ class UUID:
raise TypeError('UUID objects are immutable')
def __str__(self):
- hex = '%032x' % self.int
- return '%s-%s-%s-%s-%s' % (
- hex[:8], hex[8:12], hex[12:16], hex[16:20], hex[20:])
+ x = self.hex
+ return f'{x[:8]}-{x[8:12]}-{x[12:16]}-{x[16:20]}-{x[20:]}'
@property
def bytes(self):
@@ -387,7 +386,7 @@ class UUID:
@property
def hex(self):
- return '%032x' % self.int
+ return self.bytes.hex()
@property
def urn(self):
diff --git a/Misc/NEWS.d/next/Library/2025-03-13-19-53-57.gh-issue-131196.3sBFv2.rst b/Misc/NEWS.d/next/Library/2025-03-13-19-53-57.gh-issue-131196.3sBFv2.rst
new file mode 100644
index 0000000..72c10fe
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-03-13-19-53-57.gh-issue-131196.3sBFv2.rst
@@ -0,0 +1 @@
+Improve perfomance of :attr:`uuid.UUID.hex` and :meth:`uuid.UUID.__str__ <object.__str__>`.