[New-bugs-announce] [issue27704] bytes(x) is slow when x is bytearray

INADA Naoki report at bugs.python.org
Sun Aug 7 15:22:27 EDT 2016


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 <report at bugs.python.org>
<http://bugs.python.org/issue27704>
_______________________________________


More information about the New-bugs-announce mailing list