Skip to content

bytearray slice assignment for bytes-like object is much slower than that for bytearray #101444

Open
@msoxzw

Description

@msoxzw

Bug report

bytearray slice assignment for bytes-like object is much slower than that for bytearray

On my machine:
python -m timeit -s "a=bytearray(4096);b=b'x'*1024;" "a[:1024]=b"
1000000 loops, best of 5: 296 nsec per loop
python -m timeit -s "a=bytearray(4096);b=memoryview(b'x'*1024);" "a[:1024]=b"
1000000 loops, best of 5: 293 nsec per loop
python -m timeit -s "a=bytearray(4096);b=bytearray(b'x'*1024);" "a[:1024]=b"
5000000 loops, best of 5: 80.4 nsec per loop

On official Python playground(https://www.python.org/shell/):
import timeit
timeit.main(["-s", "a=bytearray(4096);b=b'x'*1024;", "a[:1024]=b"])
1000000 loops, best of 5: 272 nsec per loop
timeit.main(["-s", "a=bytearray(4096);b=memoryview(b'x'*1024);", "a[:1024]=b"])
1000000 loops, best of 5: 268 nsec per loop
timeit.main(["-s", "a=bytearray(4096);b=bytearray(b'x'*1024);", "a[:1024]=b"])
5000000 loops, best of 5: 78.8 nsec per loop

Your environment

  • CPython versions tested on: 3.11.1
  • Operating system and architecture: Windows 10 22H2 x86-64

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions