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

倖福魔咒の 提交于 2019-12-24 01:25:47

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/36968157/what-is-the-fastest-way-to-copy-a-2d-array-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!