New submission from INADA Naoki: When bytes(x), bytes_new checks if x is integer via PyNumber_AsSize_t(x). It cause TypeError internally.
When x is not an integer, especially bytearray or memoryview, the internal exception cause significant overhead. # HEAD $ ./python -m timeit -s 'data=bytearray(b"xyz")' 'bytes(data)' 1000000 loops, best of 3: 0.696 usec per loop $ ./python -m timeit -s 'data=bytearray(b"xyz")' 'bytes(data)' 1000000 loops, best of 3: 0.699 usec per loop $ ./python -m timeit -s 'data=bytearray(b"xyz")' 'bytes(data)' 1000000 loops, best of 3: 0.701 usec per loop # this patch $ ./python -m timeit -s 'data=bytearray(b"xyz")' 'bytes(data)' 1000000 loops, best of 3: 0.265 usec per loop $ ./python -m timeit -s 'data=bytearray(b"xyz")' 'bytes(data)' 1000000 loops, best of 3: 0.265 usec per loop $ ./python -m timeit -s 'data=bytearray(b"xyz")' 'bytes(data)' 1000000 loops, best of 3: 0.267 usec per loop ---------- components: Interpreter Core files: fast-bytearray-fromobject.patch keywords: patch messages: 272130 nosy: methane priority: normal severity: normal status: open title: bytes(x) is slow when x is bytearray type: performance versions: Python 3.6 Added file: http://bugs.python.org/file44039/fast-bytearray-fromobject.patch _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue27704> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com