4

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
  • What kind of array? NumPy ndarray? python array? nested python list? some other array?
    – MSeifert
    May 1, 2016 at 16:08

2 Answers 2

14

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.