Created
December 11, 2011 11:41
-
-
Save wraithan/1460132 to your computer and use it in GitHub Desktop.
deepcopy vs list comprension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| """ |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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