You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
defencode(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.appenddefadd_encode(obj):
"""Encode an object, appending bytes to `binary` list."""ifisinstance(obj, bytes):
append(b'%i:%b'% (len(obj), obj))
elifisinstance(obj, memoryview):
append(b'%i:%b'% (len(obj), obj.tobytes()))
elifisinstance(obj, str):
obj_bytes=obj.encode('utf-8')
append(b"%i:%b"% (len(obj_bytes), obj_bytes))
elifisinstance(obj, int):
append(b"i%ie"%obj)
elifisinstance(obj, (list, tuple)):
append(b"l")
foriteminobj:
add_encode(item)
append(b'e')
elifisinstance(obj, dict):
append(b'd')
try:
forkey, valueinsorted(obj.items(), key=itemgetter(0)):
append(b"%i:%b"% (len(key), key))
add_encode(value)
exceptTypeError:
raiseEncodeError('dict keys must be bytes')
append(b'e')
else:
raiseEncodeError(
'value {!r} can not be encoded in Bencode'.format(obj)
)
add_encode(obj)
returnb''.join(binary)
The text was updated successfully, but these errors were encountered:
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.
The text was updated successfully, but these errors were encountered: