8000 gh-131196: Improve perfomance of `UUID.hex` and `UUID.__str__` by ~10… · python/cpython@1121c80 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1121c80

Browse files
booqoffskypicnixz
andauthored
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>
1 parent d7d2289 commit 1121c80

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

Lib/uuid.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,8 @@ def __setattr__(self, name, value):
321321
raise TypeError('UUID objects are immutable')
322322

323323
def __str__(self):
324-
hex = '%032x' % self.int
325-
return '%s-%s-%s-%s-%s' % (
326-
hex[:8], hex[8:12], hex[12:16], hex[16:20], hex[20:])
324+
x = self.hex
325+
return f'{x[:8]}-{x[8:12]}-{x[12:16]}-{x[16:20]}-{x[20:]}'
327326

328327
@property
329328
def bytes(self):
@@ -387,7 +386,7 @@ def node(self):
387386

388387
@property
389388
def hex(self):
390-
return '%032x' % self.int
389+
return self.bytes.hex()
391390

392391
@property
393392
def urn(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve perfomance of :attr:`uuid.UUID.hex` and :meth:`uuid.UUID.__str__ <object.__str__>`.

0 commit comments

Comments
 (0)
0