Skip to content

Instantly share code, notes, and snippets.

@wraithan
Created December 11, 2011 11:41
Show Gist options
  • Select an option

  • Save wraithan/1460132 to your computer and use it in GitHub Desktop.

Select an option

Save wraithan/1460132 to your computer and use it in GitHub Desktop.
deepcopy vs list comprension
from copy import deepcopy
from timeit import Timer
list_o_lists = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
def deepcopy_f():
meh = deepcopy(list_o_lists)
def slice_f():
meh = [li[:] for li in list_o_lists]
deepcopy_t = Timer("deepcopy_f()", "from __main__ import deepcopy_f, list_o_lists")
slice_t = Timer("slice_f()", "from __main__ import slice_f, list_o_lists")
print("deepcopy: %s" % deepcopy_t.timeit(number=1000))
print("slice: %s" % slice_t.timeit(number=1000))
"""OSX 10.7.2 pypy 1.7.0
deepcopy: 0.0895478725433
slice: 0.00567483901978
"""
"""OSX 10.7.2 python 2.7.2
deepcopy: 0.0501799583435
slice: 0.00181603431702
"""
@wraithan

Copy link
Copy Markdown
Author

1 million reps

OSX 10.7.2 pypy 1.7.0
deepcopy: 4.24665093422
slice: 0.431082963943

OSX 10.7.2 cpython 2.7.2
deepcopy: 48.4800450802
slice: 1.80412483215

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment