3 ms·
I wonder if bytearray([97,98,99]).decode('latin-1') is faster still. Edit: Yup, it is. 3x faster. https://gist.github.com/anonymous/18e372e8d0173e77b5c405920d4
by tyingq 10y ago
I wonder if bytearray([97,98,99]).decode('latin-1') is faster still.
Edit: Yup, it is. 3x faster. https://gist.github.com/anonymous/18e372e8d0173e77b5c405920d4d3080 https://gist.github.com/anonymous/18e372e8d0173e77b5c405920d...
- sametmax 10y agoWas thinking exactly the same. Plus you save an import. And no need to wrap it in a function since it's a short method call on a built-in. Would bytearray([97,98,99]).decode('ascii') be even faster ?
- tyingq 10y agoIt would fail when you pass it 128. He's passing in values from 0-255. "Ascii" is sort of a misnomer in his post.
- jstimpfle 10y agoIt's better to fail than to convert something that probably isn't latin1!
- tyingq 10y agoWell, it's not clear what behavior he was looking for. And not possible to compare speed without an encoding that works for 0-255.
- mark-r 10y agoThe latin1 codec doesn't fail for any values in the 0-255 range. It probably should, since there are values that don't map to a valid character. I don't know if this is deliberate and guaranteed or just an artifact of the current implementation. There should have been a 'byte' encoding that was explicitly made for 1:1 conversions.
- jstimpfle 10y agoEven shorter: bytes([97,89,99]).decode()
- tyingq 10y agoWorks on python v3, but not 2.
- jMyles 10y agoIt's arguably marginally more readable as well.