Deepcopying a byte string is quicker than copying it - problem?

F

Frank Millman

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?

Frank Millman
 
F

Florian Leitner

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?

Frank Millman

Indeed, just tried this:

Python 3.3.2 (default, Sep 30 2013, 21:37:29)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.75)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
import copy

bs = b'a'*1000 id(bs) 140246553640960
id(copy.copy(bs)) # WATCH THIS! 140246578636800
id(copy.deepcopy(bs)) 140246553640960
id(bs[:]) 140246553640960

us = 'a'*1000 id(us) 140246553642496
id(copy.copy(us)) 140246553642496
id(copy.deepcopy(us)) 140246553642496
id(us[:])
140246553642496

Interesting; copy.copy(b'bytes') creates a "real" copy of an otherwise
immutable value... That might actually be some form of a bug.

By the way, if you are looking into this as something about speed, you
should use slices, not copy.copy/deepcopy, anyways (copy = bc[:]);
More than 5x faster:

$ python -m timeit -s "import copy; s='a'*1000" "copy.copy(s)"
1000000 loops, best of 3: 0.465 usec per loop
$ python -m timeit -s "import copy; s='a'*1000" "cs = s[:]"
10000000 loops, best of 3: 0.0728 usec per loop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top