Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 200
Delgan Avatar asked Dec 25 '22 06:12

Delgan


1 Answers

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.

like image 154
Delgan Avatar answered Dec 26 '22 19:12

Delgan