-
Notifications
You must be signed in to change notification settings - Fork 10
Closed
Description
Hi,
I've discovered that my pure Python implementation of bencode encode is faster than your Cython one. Depending on what is being encoded it can range from a few percent faster to several orders of magnitude faster. Not sure why that is the case, I would expect the Cython version to be faster in all cases.
Here's my bencode encode function.
def encode(obj):
"""
Encode data in to bencode, return bytes.
The following objects may be encoded: int, bytes, list, dicts.
Dict keys must be bytes, and unicode strings will be encoded in to
utf-8.
"""
binary = []
append = binary.append
def add_encode(obj):
"""Encode an object, appending bytes to `binary` list."""
if isinstance(obj, bytes):
append(b'%i:%b' % (len(obj), obj))
elif isinstance(obj, memoryview):
append(b'%i:%b' % (len(obj), obj.tobytes()))
elif isinstance(obj, str):
obj_bytes = obj.encode('utf-8')
append(b"%i:%b" % (len(obj_bytes), obj_bytes))
elif isinstance(obj, int):
append(b"i%ie" % obj)
elif isinstance(obj, (list, tuple)):
append(b"l")
for item in obj:
add_encode(item)
append(b'e')
elif isinstance(obj, dict):
append(b'd')
try:
for key, value in sorted(obj.items(), key=itemgetter(0)):
append(b"%i:%b" % (len(key), key))
add_encode(value)
except TypeError:
raise EncodeError('dict keys must be bytes')
append(b'e')
else:
raise EncodeError(
'value {!r} can not be encoded in Bencode'.format(obj)
)
add_encode(obj)
return b''.join(binary)
Metadata
Metadata
Assignees
Labels
No labels