简体   繁体   中英

What is the fastest way to copy a 2D array in Python?

I have to make a very large number of simulations on a R*C grid.

These simulations are altering the grid, so I need to copy my reference grid before each, and then apply my simulating function on the fresh new grid.

What is the fastest way to do this in Python?


Since I have not found a similar question on StackOverflow, I did the tests myself and decided to post them here thinking they could be useful to other people.

The answer will be a community response so that other people can add new measurements with possibly other techniques.

If you add another method, remember to measure all the old tests and update them because the time depends on the computer used, avoid biasing the results.

I used a bash variable for setting up the timeit tests:

setup="""
R = 100
C = 100
from copy import deepcopy
import numpy as np
ref = [[i for i in range(C)] for _ in range(R)]
ref_np = np.array(ref)
cp = [[100 for i in range(C)] for _ in range(R)]
cp_np = np.array(cp)
"""

Just for convenience, I also set a temporary alias pybench :

alias pybench='python3.5 -m timeit -s "$setup" $1'

Python 3

Python 3.5.0+ (default, Oct 11 2015, 09:05:38)

  • Deepcopy:

     >>> pybench "cp = deepcopy(ref)" 100 loops, best of 3: 8.29 msec per loop 
  • Modifying pre-created array using index:

     >>> pybench \\ "for y in range(R): for x in range(C): cp[y][x] = ref[y][x]" 1000 loops, best of 3: 1.16 msec per loop 
  • Nested list comprehension:

     >>> pybench "cp = [[x for x in row] for row in ref]" 1000 loops, best of 3: 390 usec per loop 
  • Slicing:

     >>> pybench "cp = [row[:] for row in ref]" 10000 loops, best of 3: 45.8 usec per loop 
  • NumPy copy:

     >>> pybench "cp_np = np.copy(ref_np)" 100000 loops, best of 3: 6.03 usec per loop 
  • Copying to pre-created NumPy array:

     >>> pybench "np.copyto(cp_np, ref_np)" 100000 loops, best of 3: 4.52 usec per loop 

There is nothing very surprising in these results, as you might have guessed, use NumPy is enormously faster, especially if one avoids creating a new table each time.

To add to the answer from Delgan, numpy copy's documentation says to use numpy.ndarray.copy as the preferred method. So for now, without doing a timing test, I will use numpy.ndarray.copy

https://numpy.org/doc/stable/reference/generated/numpy.copy.html

https://numpy.org/doc/stable/reference/generated/numpy.ndarray.copy.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM