mirror of
https://github.com/python/cpython.git
synced 2025-10-15 11:22:18 +00:00
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>
This commit is contained in:
parent
d7d22899e2
commit
1121c80fda
2 changed files with 4 additions and 4 deletions
|
@ -321,9 +321,8 @@ class UUID:
|
||||||
raise TypeError('UUID objects are immutable')
|
raise TypeError('UUID objects are immutable')
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
hex = '%032x' % self.int
|
x = self.hex
|
||||||
return '%s-%s-%s-%s-%s' % (
|
return f'{x[:8]}-{x[8:12]}-{x[12:16]}-{x[16:20]}-{x[20:]}'
|
||||||
hex[:8], hex[8:12], hex[12:16], hex[16:20], hex[20:])
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def bytes(self):
|
def bytes(self):
|
||||||
|
@ -387,7 +386,7 @@ class UUID:
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def hex(self):
|
def hex(self):
|
||||||
return '%032x' % self.int
|
return self.bytes.hex()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def urn(self):
|
def urn(self):
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Improve perfomance of :attr:`uuid.UUID.hex` and :meth:`uuid.UUID.__str__ <object.__str__>`.
|
Loading…
Add table
Add a link
Reference in a new issue