Deepcopying a byte string is quicker than copying it - problem?
Peter Otten
__peter__ at web.de
Thu Feb 27 03:25:22 EST 2014
Frank Millman wrote:
> Hi all
>
> I noticed this a little while ago, but dismissed it as a curiosity. On
> reflection, I decided to mention it here in case it indicates a problem.
>
> This is with python 3.3.2.
>
> C:\>python -m timeit -s "import copy" "copy.copy('a'*1000)"
> 100000 loops, best of 3: 6.91 usec per loop
>
> C:\>python -m timeit -s "import copy" "copy.deepcopy('a'*1000)"
> 100000 loops, best of 3: 11.8 usec per loop
>
> C:\>python -m timeit -s "import copy" "copy.copy(b'a'*1000)"
> 10000 loops, best of 3: 79.9 usec per loop
>
> C:\>python -m timeit -s "import copy" "copy.deepcopy(b'a'*1000)"
> 100000 loops, best of 3: 11.7 usec per loop
>
> As you can see, deepcopying a string is slightly slower than copying it.
>
> However, deepcopying a byte string is orders of magnitude quicker than
> copying it.
>
> Actually, looking closer, it is the 'copy' that is slow, not the
> 'deepcopy' that is quick..
>
> Expected, or odd?
Definitely odd. For some reason deepcopy() special-cases the bytes type
while copy() does not. If you fix that:
$ python3.4 -m timeit -s 'import copy; b = b"a"*1000' 'copy.copy(b)'
10000 loops, best of 3: 21.2 usec per loop
$ python3.4 -m timeit -s 'import copy; copy._copy_dispatch[bytes] =
copy._copy_immutable; b = b"a"*1000' 'copy.copy(b)'
1000000 loops, best of 3: 0.971 usec per loop
I think this an oversight rather than intentional. Please report to
bugs.python.org.
More information about the Python-list
mailing list