Author pitrou
Recipients pitrou
Date 2013-09-25.14:03:49
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1380117830.08.0.43327617783.issue19087@psf.upfronthosting.co.za>
In-reply-to
Content
If you delete a slice at the end of a bytearray, it is naturally optimized (thanks to the resizing strategy). However, if you delete a slice at the front of a bytearray, it is not: a memmove() gets done every time.

$ ./python -m timeit "b=bytearray(10000)" "while b: b[-1:] = b''"
100 loops, best of 3: 5.67 msec per loop
$ ./python -m timeit "b=bytearray(10000)" "while b: b[:1] = b''"
100 loops, best of 3: 6.67 msec per loop

$ ./python -m timeit "b=bytearray(50000)" "while b: b[-1:] = b''"
10 loops, best of 3: 28.3 msec per loop
$ ./python -m timeit "b=bytearray(50000)" "while b: b[:1] = b''"
10 loops, best of 3: 61.1 msec per loop

$ ./python -m timeit "b=bytearray(100000)" "while b: b[-1:] = b''"
10 loops, best of 3: 59.4 msec per loop
$ ./python -m timeit "b=bytearray(100000)" "while b: b[:1] = b''"
10 loops, best of 3: 198 msec per loop

This makes implementing a fifo using bytearray a bit suboptimal. It shouldn't be very hard to improve.
History
Date User Action Args
2013-09-25 14:03:50pitrousetrecipients: + pitrou
2013-09-25 14:03:50pitrousetmessageid: <1380117830.08.0.43327617783.issue19087@psf.upfronthosting.co.za>
2013-09-25 14:03:50pitroulinkissue19087 messages
2013-09-25 14:03:49pitroucreate