Make util.XOR about ten times faster by calling bytes only once.
This commit is contained in:
parent
7ea121b115
commit
2587d82af8
@ -1,3 +1,4 @@
|
|||||||
|
import builtins
|
||||||
import sys
|
import sys
|
||||||
import hashlib
|
import hashlib
|
||||||
|
|
||||||
@ -22,7 +23,6 @@ def bytes(text):
|
|||||||
if text is None:
|
if text is None:
|
||||||
return b''
|
return b''
|
||||||
|
|
||||||
import builtins
|
|
||||||
if isinstance(text, builtins.bytes):
|
if isinstance(text, builtins.bytes):
|
||||||
# We already have bytes, so do nothing
|
# We already have bytes, so do nothing
|
||||||
return text
|
return text
|
||||||
@ -80,10 +80,9 @@ def XOR(x, y):
|
|||||||
:param bytes y: A byte string
|
:param bytes y: A byte string
|
||||||
:rtype: bytes
|
:rtype: bytes
|
||||||
"""
|
"""
|
||||||
result = b''
|
# This operation is faster with a list comprehension than with a
|
||||||
for a, b in zip(x, y):
|
# generator, as of 2016 on python 3.5.
|
||||||
result += bytes([a ^ b])
|
return builtins.bytes([a ^ b for a, b in zip(x, y)])
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
def hash(name):
|
def hash(name):
|
||||||
|
Loading…
Reference in New Issue
Block a user